TextBox tb1, tb2, tb3, tb4; ComboBox cb; //initialize text box //todo... var tbArray = new Control[] { tb1, tb2, tb3, tb4, cb }; var msg = string.Join(",", (from ctrl in tbArray where string.IsNullOrEmpty(ctrl.Text) select ctrl.Name)); if (!string.IsNullOrEmpty(msg)) { MessageBox.Show(msg); }
那就用第二种办法,遍历它们的父控件的所有子控件,当然用这个方法的前提是它们必须都在同一个父控件里 我现在假设它们都在Form里foreach(Control c in this.Controls) { if(c is TextBox || c is ComboBox) { if(string.IsNullOrEmpty(c.Text)) MessageBox.Show(c.Name); } }
foreach(TextBox tb in textboxes)
{
if(string.IsNullOrEmpty(tb.Text))
{
MessageBox.Show(tb.Name);
}
}当然,遍历这4个TextBox的父控件的所有子控件也是可以的
if (rbs.Any(x => x.Text == ""))
{
tbs.Where(x => x.Text == "").Tolist().ForEach(x => MessageBox.Show(x.Name));
}
这个该怎么办啊
TextBox tb1, tb2, tb3, tb4;
ComboBox cb;
//initialize text box
//todo...
var tbArray = new Control[] { tb1, tb2, tb3, tb4, cb };
var msg = string.Join(",", (from ctrl in tbArray where string.IsNullOrEmpty(ctrl.Text) select ctrl.Name));
if (!string.IsNullOrEmpty(msg))
{
MessageBox.Show(msg);
}
我现在假设它们都在Form里foreach(Control c in this.Controls)
{
if(c is TextBox || c is ComboBox)
{
if(string.IsNullOrEmpty(c.Text))
MessageBox.Show(c.Name);
}
}
if (rbs.Any(x => x.Text == ""))
{
tbs.Where(x => x.Text == "").Tolist().ForEach(x => MessageBox.Show(x.Name));
}
实际情况是FORM里面有很多TextBox和Combox 而我只需要判断其中3个TextBox和1个Combox
这个如何控制...