Click here to Skip to main content
15,791,892 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I made an array of toolsstripsbutton successfully the problem is that I want to assign a diffrent click event for each of them. Note I know how to make the click event for them i Just wanna assign a diffrent event for each
Posted
Updated 22-Oct-12 10:32am
v2
Comments
Thomas Daniels 22-Oct-12 12:22pm    
How many ToolStripButtons are in the array, or is the count changing?
Member 8991168 22-Oct-12 12:48pm    
There are about 100 array of toolstripbuttons and anther 100 array of webbrowsers what I want to do is for example when I press on toolstripbutton [1] it it shows webbrowser [1], in anthother way I want to make my custom tabbed browser
Sergey Alexandrovich Kryukov 22-Oct-12 12:50pm    
What do you mean "assign event"? There is no such thing. You declare the event, and then the user code adds a handler to the invocation list of an event handler.
If you really knew that, it would not make any difference to you.
--SA
Maciej Los 22-Oct-12 13:48pm    
My virtual 5!
Sergey Alexandrovich Kryukov 22-Oct-12 14:40pm    
My real thank you. :-)
--SA

 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 22-Oct-12 14:41pm    
Even though this set should overwhelm OP, my 5. :-)
--SA
Maciej Los 22-Oct-12 15:23pm    
Thank you, Sergey ;)
Sergey Alexandrovich Kryukov 22-Oct-12 14:53pm    
Actually, OP is confused with something related to event handling. I tried to give more focused answer, please see (Solution 2).
--SA
Sandeep Mewara 22-Oct-12 15:38pm    
My 5! Good set of links.
Maciej Los 22-Oct-12 15:40pm    
Thank you, Sandeep ;)
OK, you are given some useful links by Maceiej. Let me give you some bottom line. Basically, you apply "+=" operators to each instance of the button once or more to add some event handlers to add to the invocation list of the Click event of each. Each event handler is itself an instance of the delegate of appropriate method signature. I would recommend using anonymous methods in all cases. You can combine sharing of the same handlers by different instances of control with individual handlers. This is totally up to you:
C#
myButton.Click += (sender, eventArgs) => {
    someSharedMethod();
}

myOtherButton.Click += (sender, eventArgs) => {
    ToolStripButton button = (ToolStripButton).sender;
    // if myOtherButton is ToolStripButton, 
    // the line above should give you the same reference as myOtherButton
    // it's better to get it from sender though,
    // to avoid unnecessary closure (please see below)
    someSharedMethodDepedingOnControlInstance(button);
}

myYetAnotherButton.Click += (sender, eventArgs) => {
    someIndividualMethod();
}


As you can see, using anonymous methods adds some flexibility: you don't need to create special handler method with both parameters, as one or both of them are often unused. Now decide what to do with all that by yourself, depending on your code design.

[EDIT]

If you are confused with my note on closures (which is likely, please see above), please also see:
http://en.wikipedia.org/wiki/Closure_%28computer_science%29#Delegates_.28C.23.2C_D.29[^].

—SA
 
Share this answer
 
v5
Comments
Abhishek Pant 22-Oct-12 15:10pm    
Ulimate Sir.. Real 6/5
Sergey Alexandrovich Kryukov 22-Oct-12 15:27pm    
Thank you, Abhishek.
--SA
Maciej Los 22-Oct-12 15:29pm    
I didn't "see" your vote...
Sergey Alexandrovich Kryukov 22-Oct-12 15:31pm    
Thank you for the reminder, Maciej.
Anyway, good words from members themselves are way more valuable...
--SA
Maciej Los 22-Oct-12 15:23pm    
Wow! +5

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