I want to define a new event for a general button control or any other my defined controls in c#. how can do it?
Asked
Active
Viewed 116 times
0
-
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 Answers
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.
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<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
}

Random Dev
- 51,810
- 9
- 92
- 119
-
-
-
Now there is a ; missing after the < ;-) 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