我希望能够在多行 TextBox
中设置行数。
我试过以下方法:
int initHeight = textBox1.Height;
textBox1.Height = initHeight * numOfLines;
但是当 numOfLines
变大时,这会使它变得太大。然后我尝试了这个:
float fontHeight = textBox1.CreateGraphics().MeasureString("W", textBox1.Font).Height;
textBox1.Height = fontHeight * numOfLines;
但是当 numOfLines
小时,这太小了,而当 numOfLines
很大时,这又太大了。
所以我做错了什么……有什么想法吗?
最佳答案
这将设置多行文本框的确切 Width
和 Height
:
Size size = TextRenderer.MeasureText(textBox1.Text, textBox1.Font);
textBox1.Width = size.Width;
textBox1.Height = size.Height + Convert.ToInt32(textBox1.Font.Size);
关于c# - .NET Multiline TextBox - 设置行数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17072407/