Click here to Skip to main content
15,895,667 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
I've just created a FOR where It creates multiple buttons:

789
456
123

But they all share the same event.
How do I assign multiple events?

The buttons are created dynamicaly.
Posted
Updated 3-Jul-12 3:23am
v2
Comments
Sergey Alexandrovich Kryukov 3-Jul-12 13:30pm    
No, they do not "share the same event". It's possible that the same event handler is added to the invocation list of the Click event instance of multiple buttons, which is a usual thing.

You do not assign events -- there is no such think.
Most likely, you mean that adding an event handler to an event invocation list.

So, what's the problem? It all is done using += operator. Is that the answer?
--SA

Probably the only problem is that you don't understand what events do and how they are related to event handlers. Please see my comment to the question.

Suppose you have some event(s) declared in some class (in your case, this is Button, but I'm showing a quite general pattern):
C#
class MyEventArguments : System.EventArgs {/*...*/}

class MyClass {
    internal event System.EventHandler<MyEventArguments> SomethingIsHappening;
    internal event System.EventHandler<MyEventArguments> SomethingHappened;
    //
    //...
    //invocation can only be in declaring class, nowhere else;
    //with buttons, this is already done in the button class:
            if (SomethingIsHappening != null)
                SomethingIsHappening.Invoke(this, new MyEventArguments(/*...*/));
    //...
} //class MyClass


Pay attention for naming conventions.
To events instances are shown. All delegate and event instances are multi-cast: they have an invocation list, which is a list of delegate instances, each element carrying the address of a handler method and call parameters "this", which is a reference to the object of a class or structure to be passed to a handler method for instance (that is, non-static) methods. Through invocation, all handlers are called one by one, and they are agnostic to each other. You just add them and expect to be called on some events. In case of a button, such event is its click.

Having these declaration
C#
MyClass MyFirstInstance = new MyClass(//...
MyClass MySecondInstance = new MyClass(//...

void MyFirstHandler(object sender, MyEventArguments eventArgs) {/*...*/}
void MySecondHandler(object sender, MyEventArguments eventArgs) {/*...*/}

You can add different event handlers to the same or different event instances or add same event handler to different event instances, same or different events of the same instance of a declaring class/structure, in any combinations:
C#
MyFirstInstance.SomethingIsHappening += MyFirstHandler;
MySecondInstance.SomethingHappened += MyFirstHandler;
MyFirstInstance.SomethingIsHappening += MySecondInstance;
MyFirstInstance.SomethingIsHappening += delegate(object sender, MyEventArguments eventArgs) {/*...*/} //anonymous
//with .NET version greater than 2.0 you can also use more convenient lambda syntax:
MyFirstInstance.SomethingIsHappening += (sender, eventArgs) => {/*...*/} //no need to write parameter types, the are inferred from event type
//..., etc.


—SA
 
Share this answer
 
v2
Comments
Espen Harlinn 3-Jul-12 16:12pm    
Good effort :-D
Sergey Alexandrovich Kryukov 3-Jul-12 17:06pm    
Thank you, Espen.
--SA
Steven Borges 6-Jul-12 6:27am    
Thank you, this cleared a lot of my doubts.
Sergey Alexandrovich Kryukov 11-Jul-12 16:05pm    
You are welcome.
If so, consider accepting the answer formally (green button) -- thanks.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900