现在的位置: 首页 > 综合 > 正文

c# + linq 判定GroupBox上的所有TextBox控件的Text为空

2013年10月09日 ⁄ 综合 ⁄ 共 548字 ⁄ 字号 评论关闭

一个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;       }  

 }

}

抱歉!评论已关闭.