日期:2014-05-18  浏览次数:20762 次

c# 一个form有多个控件,检测用户是否输入为空,用ErrorProvider提示错误,怎么提取成一个方法来做?
不想写很多if来判断
private List<Control> errManager()
{
  List<Control> controls = null;

  foreach (Control control in this.Controls)
  {
  /////////////////////////////////////////////////////////////
  }

   
  return controls;
}

这样的思路是对的吗?希望补充为完成代码,谢谢!

------解决方案--------------------
no,sender就ok
------解决方案--------------------
你这种应该可以了,获得列表,就遍历吧
------解决方案--------------------
遍历控件
------解决方案--------------------
foreach (Control control in this.Controls)
{
if(control.getType()==typeof(TextBox))
{
TextBox txt=control as TextBox;
if(txt.Text.Lenght==0)
{
errorProvider1.SetError(txt, "不能为空");
}
else
{
errorProvider1.SetError(txt, "");
}
}
}

------解决方案--------------------
可以呀 这样做挺好的 我也这么做过
------解决方案--------------------
探讨
foreach (Control control in this.Controls)
{
if(control.getType()==typeof(TextBox))
{
TextBox txt=control as TextBox;
if(txt.Text.Lenght==0)
{
errorProvider1.SetError(txt, "不能为空")……