how to resize the textbox control with runtime events in c# using c# winforms

Something like:

textBox1.KeyPress += new KeyPressEventHandler(textBox1_KeyPress); 

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            textBox1.Width = 20;
        }

Thanks

Something like:

textBox1.KeyPress += new KeyPressEventHandler(textBox1_KeyPress); 

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            textBox1.Width = 20;
        }

Thanks

this is only for fixed size. i need default size

Use this:

public partial class Form1 : Form
    {
        // Default size
        int defaultWidth = 100;

        public Form1()
        {
            InitializeComponent();

            textBox1.KeyPress += new KeyPressEventHandler(textBox1_KeyPress); 

        }

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            textBox1.Width = defaultWidth;
        }
}

Here is an example of making a textbox grow wider if the input is longer than the controls width:

private void textBox1_TextChanged(object sender, EventArgs e)
    {
      using (Graphics g = textBox1.CreateGraphics())
      {
        textBox1.Width = Convert.ToInt32(Math.Max(textBox1.Width, Math.Ceiling(g.MeasureString(textBox1.Text, textBox1.Font).Width)));
      }
    }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.