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; } }
source share