Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Growing a TextBox to fit text

Growing a TextBox to fit text

Scheduled Pinned Locked Moved C#
csharphelp
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    BennyMac
    wrote on last edited by
    #1

    Dear experts, I am trying to create a class that is derived from TextBox that has the ability to automatically resize as text is added to it. It is for use within a diagramming style application. When the TextBox is created it will have a default width and height (Height is set to allow one line of text to be visible). As the user enters text and reaches the right hand side of the TextBox, I need to increase the height of the text box to allow the second line to be seen and so on. The reverse behaviour is required as text is removed, the box must resize to be the minimal size required to show all lines. Also, the TextBox has the multiline property set to TRUE. I cannot use scrollbars as the user must be able to see all text all once. I've checked the .NET documentation and have not been able to locate a property or event that will allow me to detect this situation and take the appropriate action. Even the textbox underneath icons on the Windows desktop has the behaviour that I need! If there was anyway of re-using this, it would be great! Please help! Thanks in advance ;-) Benjamin

    D 1 Reply Last reply
    0
    • B BennyMac

      Dear experts, I am trying to create a class that is derived from TextBox that has the ability to automatically resize as text is added to it. It is for use within a diagramming style application. When the TextBox is created it will have a default width and height (Height is set to allow one line of text to be visible). As the user enters text and reaches the right hand side of the TextBox, I need to increase the height of the text box to allow the second line to be seen and so on. The reverse behaviour is required as text is removed, the box must resize to be the minimal size required to show all lines. Also, the TextBox has the multiline property set to TRUE. I cannot use scrollbars as the user must be able to see all text all once. I've checked the .NET documentation and have not been able to locate a property or event that will allow me to detect this situation and take the appropriate action. Even the textbox underneath icons on the Windows desktop has the behaviour that I need! If there was anyway of re-using this, it would be great! Please help! Thanks in advance ;-) Benjamin

      D Offline
      D Offline
      Don_s
      wrote on last edited by
      #2

      Benjamin, Try the following: Initialise a local field, default to the usual height of the textbox: private int _InitialHeight = 20; In the constructor of the derived class: this.Multiline = true; this._InitialHeight = this.Height; Then override the OnTextChanged Method: protected override void OnTextChanged(System.EventArgs e) { //Get graphics System.Drawing.Graphics g = this.CreateGraphics(); //Get size System.Drawing.SizeF s = g.MeasureString(this.Text, this.Font); //Get the number of line (on top of the original 1), which need to be viewed int lines = Convert.ToInt32(s.Width) / this.Width; //Work out the height the control must be to view all lines int height = (lines * Convert.ToInt32(s.Height)) + this._InitialHeight; //Set height this.Height = height; } I hope this does what you need. Let me know if you'd like elaboration on any point. Note that this does not take into account the user using the enter key.

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups