Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resize textbox and form size according to Text Length

How can I automatically increase/decrease TextBox and Windows Form size according to text Length?

like image 433
Michel Avatar asked Dec 28 '22 11:12

Michel


1 Answers

You can try overriding the OnTextChanged event, then changing the Width depending on the size of the text.

protected override OnTextChanged(EventArgs e)
{
    using (Graphics g = CreateGraphics())
    {
        SizeF size = g.MeasureString(Text, Font);
        Width = (int)Math.Ceiling(size.Width);
    }
    base.OnTextChanged(e);
}
like image 54
Connell Avatar answered Jan 04 '23 23:01

Connell