获得在TextBox的文本行数 [英] Getting the number of text lines in a TextBox

查看:167
本文介绍了获得在TextBox的文本行数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过label.But一个文本框来显示的文本行数,就是如果最后一行是空的,标签与出空行显示的行号。

I am trying to display the number of text lines in a textbox through the label.But, the thing is if the last line is empty, the label has to display the line numbers with out that empty line.

例如,如果他们是5行到最后一行作为空,则标签应显示的行数为4

For example if they are 5 line with the last line as empty, then the label should display the number of lines as 4.

谢谢..

private void txt_CurrentVinFilter_EditValueChanged(object sender, EventArgs e)
{
   labelCurrentVinList.Text = string.Format("Current VIN List ({0})",  txt_CurrentVinFilter.Lines.Length.ToString());                       
}



其实,上面的代码是...必须改变只显示没有空行的C#的WinForms。

Actually, above is the code...have to change to display only the no-empty lines in C# winforms.

感谢

推荐答案

您也可以通过这种方法使用LINQ较短的方式。要计算行和exlcude最后一行,如果它是空的:

You can also do this in a shorter way using LinQ. To count the lines and exlcude the last line if it is empty:

var lines = tb.Lines.Count();
lines -= String.IsNullOrWhiteSpace(tb.Lines.Last()) ? 1 : 0;

和计数唯一的非空行:

var lines = tb.Lines.Where(line => !String.IsNullOrWhiteSpace(line)).Count();

这篇关于获得在TextBox的文本行数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆