情况如下: 
一,有类class a {
public String B{
get{
return "s";
}
set{
throw new Exception();
}
二。
在某个winform窗体中,有textBox1 初始化如下:
a aobject = new a();
textBox1.DataBindings.Add("Text",aobject,"B");这样,在用户更改textBox1的值时,aobject内部会抛出相应的异常,可是问题是这个异常在程序中的什么位置可以捕获到?PS:纯讨论技术

解决方案 »

  1.   

    你应该实现IDataErrorInfo接口
    参见http://www.cnblogs.com/tansm/archive/2005/01/11/89972.html
      

  2.   

    定义:
       public class a:IDataErrorInfo
        {
            private string _error=String.Empty;
            private Hashtable m_propErrors = new Hashtable();        public String B
            {
                get
                {
                    return "s";
                }
                set
                {
                    m_propErrors["B"] = "错误";
                    _error = "错误";               
                }
            }        #region IDataErrorInfo 成员        public string Error
            {
                get { return _error; }
            }        public string this[string columnName]
            {
                get { return (string)m_propErrors[columnName]; }
            }        #endregion       
        }使用
    a aobject = new a();
    TextBox1.DataBindings.Add("Text", aobject, "B");
    this.errorProvider1.DataSource = aobject; //与一个ErroProvider控件关联