ErrorProvider与CheckedListBox

http://www.cnblogs.com/myshell/archive/2010/09/24/1834184.html

最近因为做WinForm的项目,遇到这个问题,当时以为CheckedListBox不能满足这个功能,所以采用了ListBox + CheckBox的组合。后来发现,CheckedListBox完全满足,但还是打算写在博客里,算是个总结。

  实现其实很简单,只是我们在通过ListBox的Controls属性添加CheckBox时,要设置CheckBox的Location值,不然,添加多个CheckBox会只显示一个。如下代码所示:

代码

string[] list = new string[] { "张三", "李四", "王五" };

int x = 0, y = 0;
foreach (string item in list)
{
      CheckBox cb = new CheckBox();
      cb.Text = item;
      cb.Location = new Point(x, y);
      clbInvisibleColumn.Controls.Add(cb);
      y += 22;
}



1、定义ErrorProvider

2、C#使用ErrorProvider的SetError方法设置需要错误提示的控件及提示方法

例如下例,因为整数不能为零,所以当输入零时,会在Text控件右边出现一个警告提示。

namespace GetNewGuid{  public partial class GetGUID : Form{  //1、ErrorProvider:提供表单上的控制项有与其相关的错误。  ErrorProvider epProvider = new ErrorProvider();  public GetGUID(){  //得到指定数量GUID事件  btnGetGUID.Click += new EventHandler(btnGetGUID_Click);  }  }  // 得到GUID按钮事件方法  private void btnGetGUID_Click(object sender, EventArgs e){  //清空错误  epProvider.Clear();  if (txtGUID.Text.Substring(0, 1) != "0"){  //……  }  else{  //2、错误提示  epProvider.SetError(txtGUID, "GUID数量只能为整数,请输入大於零的整数!");  //焦点定位到错误处  txtGUID.Focus();  //选择输入的错误  txtGUID.SelectAll();  }  }  } 同时我们也可以对ErrorProvider进行相关的设定。

region 定义ErrorProvider的相关属性

//BlinkStyle:取得或设定错误图示闪烁的速率。  epProvider.BlinkStyle = ErrorBlinkStyle.BlinkIfDifferentError;  //BlinkRate:取得或设定数值,表示错误图示何时闪烁。  epProvider.BlinkRate =50;  #endregion

posted @ 2014-03-29 01:50  zhangyongjian  阅读(205)  评论(0编辑  收藏  举报