C# 什么';当我使用errorProvider.SetIconAlignment时,目标控件上发生了一种事件
当我使用C# 什么';当我使用errorProvider.SetIconAlignment时,目标控件上发生了一种事件,c#,.net,winforms,C#,.net,Winforms,当我使用errorProvider.SetIconAlignment()方法时,有人知道目标控件上发生了什么类型的事件吗 例如: 当我调用errorProvider.SetIconAlignment(mytextbox1,ErrorIconAlignment.MiddleRight)时 在mytextbox1上,我希望在调用SetIconAlignment时捕获此事件?我没有看到ErrorProvider可能正在使用的事件。事实上,我找不到任何与此相关的事件 根据我在ErrorProvider中
errorProvider.SetIconAlignment()
方法时,有人知道目标控件上发生了什么类型的事件吗
例如:
当我调用errorProvider.SetIconAlignment(mytextbox1,ErrorIconAlignment.MiddleRight)时代码>
在mytextbox1上,我希望在调用SetIconAlignment时捕获此事件?我没有看到ErrorProvider可能正在使用的事件。事实上,我找不到任何与此相关的事件
根据我在ErrorProvider中使用的内容,除了扩展该方法之外,没有其他方法,如下所示:
// Defines an extended version of the ErrorProvider
public class ExtendedErrorProvider : ErrorProvider, INotifyPropertyChanging, INotifyPropertyChanged
{
// That will replace the SetIconAlignment from the base class when you call it from outside the class
public void SetIconAlignment(Control control, ErrorIconAlignment value)
{
// Will raise an event just before changing the property
OnPropertyChanging("IconAlignment");
// Changed the property using the base class
base.SetIconAlignment(control, value);
// Will raise an event just after the property has changed
OnPropertyChanged("IconAlignment");
}
// This will ensure that whenever you bind methods to be called on the PropertyChanging, they will get called for the specific property...
protected void OnPropertyChanging(string property) { if (PropertyChanging != null) PropertyChanging(this, new PropertyChangingEventArgs(property)); }
public event PropertyChangingEventHandler PropertyChanging;
// This will ensure that whenever you bind methods to be called on the PropertyChanged, they will get called for the specific property...
protected void OnPropertyChanged(string property) { if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(property)); }
public event PropertyChangedEventHandler PropertyChanged;
}
errorProvider1.PropertyChanging += WhatNeedsToBeDoneBeforeChanging(...);
errorProvider1.PropertyChanged += WhatNeedsToBeDoneAfterChanging(...);
现在,在本课程之外,您可以执行以下操作:
// Defines an extended version of the ErrorProvider
public class ExtendedErrorProvider : ErrorProvider, INotifyPropertyChanging, INotifyPropertyChanged
{
// That will replace the SetIconAlignment from the base class when you call it from outside the class
public void SetIconAlignment(Control control, ErrorIconAlignment value)
{
// Will raise an event just before changing the property
OnPropertyChanging("IconAlignment");
// Changed the property using the base class
base.SetIconAlignment(control, value);
// Will raise an event just after the property has changed
OnPropertyChanged("IconAlignment");
}
// This will ensure that whenever you bind methods to be called on the PropertyChanging, they will get called for the specific property...
protected void OnPropertyChanging(string property) { if (PropertyChanging != null) PropertyChanging(this, new PropertyChangingEventArgs(property)); }
public event PropertyChangingEventHandler PropertyChanging;
// This will ensure that whenever you bind methods to be called on the PropertyChanged, they will get called for the specific property...
protected void OnPropertyChanged(string property) { if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(property)); }
public event PropertyChangedEventHandler PropertyChanged;
}
errorProvider1.PropertyChanging += WhatNeedsToBeDoneBeforeChanging(...);
errorProvider1.PropertyChanged += WhatNeedsToBeDoneAfterChanging(...);
事件是好的…我在考虑更多关于观察者/可观察者的问题…但答案很好。基于ErrorProvider构建自己的扩展类是个好主意。但当错误图标设置为mytextbox1时,似乎并没有办法跟踪事件?是的,有办法。从代码项目中签出。在那里,他们使用IPropertyNotification的自定义实现来传递其他参数以及属性名称(如旧值和新值,或者在您的场景中,对象/控件本身)。