Search This Blog

Friday, January 24, 2014

Autosize Control sized based on its text C# CilentSize

C# > Controls > ClientSize

ClientSize gets or sets the height and width of the client area of the control.

Example:
Autosize TextBox width and height  based on its text.








C# Code:

 private void AutoSizeControl(Control control)
        {
            Graphics g = control.CreateGraphics();
            Size size = g.MeasureString(control.Text, control.Font).ToSize();
            control.ClientSize = new Size(size.Width+10 ,size.Height);
            g.Dispose();
        }
        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            AutoSizeControl(textBox1); 
        }