3
Contributors
4
Replies
5
Views
7 Years
Discussion Span
Last Post by sknake
0

Something like:

textBox1.KeyPress += new KeyPressEventHandler(textBox1_KeyPress); 

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

Thanks

Edited by farooqaaa: n/a

0

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

0

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

Edited by farooqaaa: n/a

0

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)));
      }
    }
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.