开发者

Definition of a new Event for a UserDefined control or common controls in c# [duplicate]

This question already has answers here: How to dispatch events in C# (4 answers) C开发者_StackOverflow社区losed 9 years ago.

I want to define a new event for a general button control or any other my defined controls in c#. how can do it?


You can subclass from the button control and define a new event.

Use the event keyword in your class, as seen in the example on the linked page.


Hope. I understant what you really mean, cause "programming skills" tag confuse me.

Here you can find a short but vey good example of how to do it.

If this is not what you're asking for, please explain better.


First derive a class from EventArgs or use a existing derived-class from it if you want to give additional infos to the eventhandler.

Than you define a event in your control:


    public event EventHandler<yourEventClass> MyEventName;

or


    public event EventHandler MyEventName;

if you don't want additional infos.

Now you should define a private method to invoke the event from your code (I don't like this public but if you need to feel free to change):


    private void InvokeMyEvent( ... parameter for your additional infos ...)
    {
       var handler = MyEventName;
       if (handler == null) return;

       handler(this, new MyEventArgs(...parameters...); // if you have additional parameters
       handler(this, EventArgs.Empty); // if you don't want additional parameters
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜