Click here to Skip to main content
15,893,266 members
Please Sign up or sign in to vote.
2.33/5 (2 votes)
See more:
hello,

i was doing some experiment on AddHandler with a few button

for an example
if i already create 2 buttons in a form
how to make sure that i i click on the 1st button it will pop out a msgBox("button 1") and button for a msgBox("button 2")

how to indentify which button was click??

What I have tried:

for i as integer = 0 to 1

dim _button as new button

with _button



end with

next

AddHandler _button.Click , address of _buttonClicked


private sub _buttonClicked ( byVal _sender as object , byVal _e as EventArgs){
'if button 1 was clicked
'MsgBox("Button 1")

'if button 2 was clicked
'MsgBox("Button 2")
}
Posted
Updated 24-May-16 4:55am
v2
Comments
Philippe Mori 24-May-16 21:18pm    
Use code blocks for code in your question. You won't get many points if you don't make some effort.
newbie1992 24-May-16 23:44pm    
i see.. sorry i was new in here
still got a lot off thing i need to learn..
thanks.. :)

You can get the Button control and its details easily.

Refer - Get the ID/Name of a button in a click event. VB.NET - Stack Overflow[^]
 
Share this answer
 
Comments
newbie1992 24-May-16 5:09am    
sorry bro.. ur answer should be rated as 5*
and btw.. thanks a lot..
Most welcome buddy. :) Keep coding and networking. :)
newbie1992 24-May-16 7:52am    
ok bro.. :)
Philippe Mori 24-May-16 21:16pm    
Solution 2 is far better. You should almost always use distinct handlers.
Got it Philippe Mori. Thanks. :)
It's quite possible, but this is a bad idea. The code using such identification would be unstable, hard to maintain. This is not how event-oriented programming works.

One problem is: you did not tag the UI framework/library you are using. However, buttons are handled very similarly in all of them. Quite generally, the signature of the delegate added to the invocation list of the Click event instance takes two parameters: object sender and another one, of one of the event argument types.

It's possible to use the same method as the event handler for several buttons. Sometimes, it could make a good design. Apparently, this is what you do, otherwise you would not need to identify anything. Now, sender is always a reference to one of the button objects. So you can perform a set of checks: if sender = myFirstButton then… if sender = mySecondButton then… But this would be the abuse.

The general approach is to add a separate event handler to each button. In this approach, the handler should better be an anonymous function. I usually advice not adding any event handlers via the designer; presently, designers create obsolete code which is harder to maintain (if WPF, the situation is a bit different, but I still would recommend to be careful with XAML approaches), but it's really up to you.

As I say, using the same even handle can be useful, but only if several buttons do "almost the same thing". The rule of thumb is: whenever you need to "identify a button", you do it wrong. The right approach is: first, think why do you want to use the same handler? The answer will always be: because the code would be very similar. Now ask yourself, what would be the difference? When you figure this out, your problem is reduced to another problem: passing this "different item". You can use button tags, but the general solution would be, for example, the dictionary with button keys, and the values use in the handler logic. Hope you can grasp the idea.

—SA
 
Share this answer
 
v2
Comments
newbie1992 24-May-16 23:41pm    
i don't really get what u mean sir?
do u mean that it is better for me to add difference handler for each button?
Sergey Alexandrovich Kryukov 25-May-16 0:27am    
I mean exactly what I mean: two techniques can be used: 1) same method used as a handler for several buttons, if the actions of those buttons are similar or easily generalized; 2) different methods, per button instance.
In all cases, it's a bad idea to identify a button.
In all cases, you should not repeat yourself.
—SA
Sergey Alexandrovich Kryukov 25-May-16 9:51am    
Thank you, Tadit.
—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