Growing a TextBox to fit text
-
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
-
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
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.