【发布时间】:2011-12-24 07:25:28
【问题描述】:
我正在编写一个程序,用户应该能够在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 的大小吗?
【问题讨论】:
-
你在 Silverlight 中尝试这个吗?
标签: c# winforms textbox resize