有没有办法向控件添加事件,例如使用 IExtenderProvider 向控件添加属性?
我尝试用 errorpovider 编写自己的验证器。使用 IExtenderProvider 我将 errorprovider 和 errortext 添加到控件中。现在我需要从我的扩展程序类中触发一个事件。
片段:
[ProvideProperty("ErrorText", typeof(TextBox))]
[ProvideProperty("ErrorProvider", typeof(TextBox))]
class ValidatorExtender : Component, IExtenderProvider {
public bool CanExtend(object extendee) {
return extendee is TextBox;
}
[DefaultValue(""), Category("Data")]
public string GetErrorText(Control control) {
//---------------------------
//Return the ErrorText
//---------------------------
}
}
public void SetErrorText(Control control, string value) {
//---------------------------
//Assigning the ErrorText
//---------------------------
}
[DefaultValue(null), Category("Data")]
public ErrorProviderEX GetErrorProvider(Control control) {
//---------------------------
//Return the ErrorProvider
//---------------------------
}
public void SetErrorProvider(Control control, ErrorProviderEX value) {
//---------------------------
//Assigning the ErrorProvider
//---------------------------
}
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
public event ValidatingHandler Validating; // -> The event I want to add to the Controls
//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
void Control_Leave(object sender, EventArgs e) {
if(Validating != null){
Validating(this, new ValidatingEventArgs());
//--------------------------
// Assign Error if necessary
//--------------------------
}
}
}