I want my textbox to fit with any text that I put into it without having scroll bars. Any idea would be greatly appreciate. Thanks.
Loading
I want my textbox to fit with any text that I put into it without having scroll bars. Any idea would be greatly appreciate. Thanks.
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Mike GoldPosted Aug 19, 2005, 1:00 AM
the TextChanged event. When you get this event, invalidate the textbox.
measure the string inside the textbox in the paint routine and resize the bounds accordingly.
Here is the code:
private void textBox1_TextChanged(object sender, System.EventArgs e)
{
string text = textBox1.Text;Invalidate(textBox1.Bounds);
}
private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e){
SizeF size = e.Graphics.MeasureString(textBox1.Text, textBox1.Font);
if (size.Width < textBox1.Font.SizeInPoints * 2){
size.Width = textBox1.Font.SizeInPoints * 2;
}
textBox1.SetBounds(textBox1.Left, textBox1.Top, (
int)size.Width + 7, textBox1.Height);}