c# - .NET Multiline TextBox - 设置行数

标签 c# .net winforms textbox

我希望能够在多行 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 很大时,这又太大了。

所以我做错了什么……有什么想法吗?

最佳答案

这将设置多行文本框的确切 WidthHeight:

 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/

相关文章:

c# - Couchbase 错误打开存储桶 - 无法引导

c# - Try/Catch 未在 ReportDocument.ExportToHttpResponse 中捕获 System.Threading.ThreadAbortException

c# - 一种使用 ThreadPool 跟踪失败工作人员的可靠方法

.net - 是否可以创建 WCF "reverse"tcp channel ?

.net - 如何检测 .Net 应用程序类型?

c# - SendKeys 未发送 '(' 和 ')'

C# - 使用 OpenXML 如何读取 Excel 文件上的空格

c++ - 允许托管环境中的托管代码回调非托管代码

c# - 在 c# 应用程序中禁用 messagebox.show()

c# - 防止多次重复选择同步控件?