我正在编写一个程序,用户应该能够在 TextBox
中编写文本。我希望 TextBox
能够自动调整大小,以适应文本内容。
我尝试了以下方法:
private void textBoxTitle_TextChanged(object sender, TextChangedEventArgs e)
{
System.Drawing.Font myFont = new System.Drawing.Font("Verdana", 8);
System.Drawing.SizeF mySize = e.Graphics.MeasureString("This is a test", myFont);
this.textBoxTitle.Width = (int)Math.Round(mySize.Width, 0);
}
我遇到一个错误,说Graphics
不能用于TextChangedEventArgs
。有没有其他方法可以调整TextBox
的大小?