一个groupBox1,其中有多个textBox,如何判断 这些textBox的Text属性全值为空?如果使用原始的,一个一个的去遍历,有的烦了~
推荐一种方法,使用linq语句解决,需加引用命名空间using System.Linq;(当然,winform窗体创建时就自动添加了):
bool empty = groupBox1.Controls.Cast<Control>().Where(c=> c is TextBox).All(c=>(cas TextBox).Text.Length==0);
还可以写成:bool empty = groupBox1.Controls.Cast<Control>().All(c=> c is TextBox&& (cas TextBox).Text.Length==0);
原始的方式:bool isEmpty;
foreach(Control cin groupBox1.Controls)
{
if(c is TextBox)
{
if(string.IsNullOrEmpty(c.Text))
{ isEmpty=true; }
else { isEmpty=false; }
}
}

本文介绍了一种使用LINQ语句高效判断WinForm中groupBox控件内多个textBox文本是否全部为空的方法,包括原始遍历方式与LINQ简洁实现,并通过实例演示了如何快速获取结果。
1131

被折叠的 条评论
为什么被折叠?



