Extensible WinForms TextBox

I created a text box in a Windows Forms application that starts from a height to enter text on one line. But I would like the text box to automatically increase its height if the user enters text that is wrapped inside the control.

I currently have multiline properties for this text box, and wordwrap is true. I tried using the TextChanged event to determine when the text was wrapped, but I cannot find any property that will help me with this. The Lines property does not provide any help with wrapped text; only for the text that the user clicked to start a new line.

How can I get my text block to expand its height every time the text wraps around the width of the text box?

+4
source share
6 answers

The same ideas as others have posted, put this in the textChanged event:

Dim s As SizeF = TextRenderer.MeasureText(txt.Text, txt.Font, txt.ClientRectangle.Size, TextFormatFlags.WordBreak) txt.Height = CInt(s.Height) 

You will need a minimum height and possibly indicate some filling, but this works.

+7
source

If you want to use the RichTextBox instead (which, in my experience, is a pretty angry control that comes with a lot of quirks), you can use the ContentResized event, which gives the new required size:

 private void HandleContentsResized(object sender, ContentsResizedEvenetArgs e) { int newheight = e.NewRectangle.Height; } 
+2
source

I just wrote this to manage the label for another project. I, as I think, got the code for the project code. Changing it in the text box should be as simple as changing the base.

 public class GrowLabel : Label { private bool _growing; //public bool GrowFontSize { get; set; } public GrowLabel() { AutoSize = false; //GrowFontSize = false; } public override sealed bool AutoSize { get { return base.AutoSize; } set { base.AutoSize = value; } } private void ResizeLabel() { if (_growing) return; try { _growing = true; var sz = new Size(Width, Int32.MaxValue); sz = TextRenderer.MeasureText(Text, Font, sz, TextFormatFlags.WordBreak); Height = sz.Height; } finally { _growing = false; } } protected override void OnTextChanged(EventArgs e) { base.OnTextChanged(e); ResizeLabel(); } protected override void OnFontChanged(EventArgs e) { base.OnFontChanged(e); ResizeLabel(); } protected override void OnSizeChanged(EventArgs e) { base.OnSizeChanged(e); ResizeLabel(); } } 
+2
source

AdamSane's post was helpful, but the text box did not increase. I would make some changes. Mods below:

 class GrowTextBox : TextBox { private double m_growIndex = 0.0; private Timer m_timer; public GrowTextBox() { AutoSize = false; this.Height = 20; // Without the timer, I got a lot of AccessViolationException in the System.Windows.Forms.dll. m_timer = new Timer(); m_timer.Interval = 1; m_timer.Enabled = false; m_timer.Tick += new EventHandler(m_timer_Tick); this.KeyDown += new KeyEventHandler(GrowTextBox_KeyDown); } void GrowTextBox_KeyDown(object sender, KeyEventArgs e) { if (e.Modifiers == Keys.Control && e.KeyCode == Keys.A) { this.SelectAll(); } } void m_timer_Tick(object sender, EventArgs e) { var sz = new Size(Width, Int32.MaxValue); sz = TextRenderer.MeasureText(Text, Font, sz, TextFormatFlags.TextBoxControl); m_growIndex = (double)(sz.Width / (double)Width); if (m_growIndex > 0) Multiline = true; else Multiline = false; int tempHeight = (int)(20 * m_growIndex); if (tempHeight <= 20) Height = 20; else Height = tempHeight; m_timer.Enabled = false; } public override sealed bool AutoSize { get { return base.AutoSize; } set { base.AutoSize = value; } } protected override void OnTextChanged(EventArgs e) { base.OnTextChanged(e); m_timer.Enabled = true; } protected override void OnFontChanged(EventArgs e) { base.OnFontChanged(e); m_timer.Enabled = true; } protected override void OnSizeChanged(EventArgs e) { base.OnSizeChanged(e); m_timer.Enabled = true; } } 
+2
source

I use the code below with success until about the 10th line, then it disconnects from 1 character, but this works for me. Do not ask me about random numbers like - 7 and - 12, they have something to do with the addition

  private void txbDescription_TextChanged(object sender, EventArgs e) { SizeF s = TextRenderer.MeasureText(txbDescription.Text, txbDescription.Font, txbDescription.ClientRectangle.Size, TextFormatFlags.TextBoxControl); int lines = (int)Math.Ceiling((decimal)Convert.ToInt32(s.Width - 7) / ((decimal)txbDescription.Width - 12)); if (lines == 0) { txbDescription.Height = 20; } else { txbDescription.Height = 20 + (lines - 1) * 13; } } 
+1
source

Unfortunately, I cannot provide the specifics, but you will probably need to complete a custom implementation.

I would get a new type of text field - ExpandableTextBox - and then you would need to implement it manually.

It also looks like what you are looking for: http://social.msdn.microsoft.com/Forums/en-US/winforms/thread/11dfb280-b113-4ddf-ad59-788f78d2995a

0
source

All Articles