0

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

AakashM
  • 62,551
  • 17
  • 151
  • 186
  • You might also want to read answers to similar questions: http://stackoverflow.com/questions/2464784/what-is-the-syntax-to-declare-an-event-in-c – tobsen Aug 22 '11 at 07:28
  • Or [this question](http://stackoverflow.com/questions/2448487/how-to-dispatch-events-in-c/2448530#2448530). – H H Aug 22 '11 at 07:40

3 Answers3

0

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.

Oded
  • 489,969
  • 99
  • 883
  • 1,009
0

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.

Oded
  • 489,969
  • 99
  • 883
  • 1,009
Tigran
  • 61,654
  • 8
  • 86
  • 123
0

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&ltyourEventClass&gt 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
    }
Random Dev
  • 51,810
  • 9
  • 92
  • 119
  • You 2nd code snipped looks like the first one. – tobsen Aug 22 '11 at 07:25
  • thanks .. allways get in trouble with < and > – Random Dev Aug 22 '11 at 07:26
  • Now there is a ; missing after the &lt ;-) Should be public event EventHandler<yourEventClass> MyEventName; But I cannot change it, because SO only allows changes containing more than 6 non-whitespace characters :( – tobsen Aug 23 '11 at 08:25