C# Resize textbox to fit content

You should try a code something like below. It has worked for me well.

private void textBox1_TextChanged(object sender, EventArgs e)
{
  Size size = TextRenderer.MeasureText(textBox1.Text, textBox1.Font);
  textBox1.Width = size.Width;
  textBox1.Height = size.Height;
}

For more information refer to TextRenderer.MeasureText()

Leave a Comment