如何扩展控件事件IExtenderProvider?

发布于 2024-11-30 05:37:27 字数 1836 浏览 1 评论 0原文

有没有办法向控件添加事件,就像使用 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
            //--------------------------
        }
    }
}

Is there a way to add events to a control like adding properties to a control with IExtenderProvider?

I try to write an own validtor with an errorpovider. with IExtenderProvider i'm adding the errorprovider and the errortext to the control. now i need to fire an event out of my extenderclass.

Snippet:

[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
            //--------------------------
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

装迷糊 2024-12-07 05:37:27

SetErrorText 方法是您的关键。您需要保留一个列表<>有错误文本的控件。当控件尚不在列表中时,可以将其添加到 SetErrorText 中的列表中。并订阅其 Validating 事件。当 value 参数为 null 或空时,您可以将其从列表中删除。并取消订阅该活动。 IExtenderProvider 的 MSDN 库文章对此进行了很好的解释,请检查那里给出的示例中 SetHelpText() 方法的代码。

您的操作方式有问题,控件可以设置错误文本,但不能设置 ErrorProvider。或者反过来说,两者都不好。最好将您自己的 ErrorProvider 保留为类的私有成员或可通过属性进行分配。一个就够了。

The SetErrorText method is your key. You need to keep a List<> of controls for which you have error text. You add the control to the list in SetErrorText when it is not already in the list. And subscribe its Validating event. You remove it from the list when the value argument is null or empty. And unsubscribe the event. This is well explained in the MSDN Library article for IExtenderProvider, check the code for the SetHelpText() method in the example given there.

There's a problem in the way you do it, a control could set the error text but not the ErrorProvider. Or the other way around, neither is good. It is best to keep your own ErrorProvider as a private member of your class or assignable through a property. One is enough.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文