I agree with OG's remarks, that it is better to use ScrollBars; however, I can't claim I understand your goal here. If you really want a change-size-while-typing TextBox, here's a clue:
With a WinForm TextBox with 'WordWrap and 'Multiline set to 'true, 'textBox1'
private int fontht, baseheight;
private void Form1_Load(object sender, EventArgs e)
{
fontht = textBox1.Font.Height;
baseheight = textBox1.Height;
}
private void TextBox1_KeyDown(object sender, KeyEventArgs e)
{
int numLines = textBox1.GetLineFromCharIndex(textBox1.TextLength);
if (numLines == 0)
{
if (textBox1.Height != baseheight)
{
textBox1.Height = baseheight;
}
return;
}
int newht = baseheight + (fontht * numLines);
if (newht == textBox1.Height) return;
textBox1.Height = newht;
}
You can improve this by taking into account TextBox properties 'Padding, and the difference between textBox1.Height and textBox1.ClientSize.Height which will give you the border width in effect. An alternative technique would use 'MeasureString.