vb.net 计算VB多行文本框中的行数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13405854/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Count number of lines in VB multiline textbox
提问by Abhay
I want to count the number of lines needed for VB multiline textbox to display the whole given string. So that I can increase height of the textbox accordingly during TextChanged event.
我想计算 VB 多行文本框显示整个给定字符串所需的行数。这样我就可以在 TextChanged 事件期间相应地增加文本框的高度。
采纳答案by Tim Schmelter
The TextBoxhas a Linesproperty.
该TextBox有一个Lines属性。
int numLines = txt.Lines.Length
But this only returns 1 during TextChanged event.
但这仅在 TextChanged 事件期间返回 1。
Then you have just one line. Lines are separated by ènvironment.NewLine (VBCrlf). Your text looks like it would have multiple lines but actually it is just wrapped since it's too long for the view.
那么你只有一行。行由 ènvironment.NewLine (VBCrlf) 分隔。您的文本看起来会有多行,但实际上它只是换行了,因为它对于视图来说太长了。
Try to set the height in TextChangedin this way:
尝试以TextChanged这种方式设置高度:
Dim s As SizeF = TextRenderer.MeasureText(txt.Text, txt.Font, txt.ClientRectangle.Size, TextFormatFlags.WordBreak)
txt.Height = CInt(s.Height)

