Same method for different buttons

M

Mark Goldin

I have a group of buttons.
When the user clicks on a button I'd like to execute
same code. Every button should send its caption to that method.
How can I do that?
 
K

Karl

Mark, I don't fully understand what you are trying to do. Each button has 1
method that can hook into it's Click event...multiple buttons can share the
same click event. Inside the click event, you can access all buttons. What
do you mean by "send its caption to that method"? As opposed to sending
information into a method, you should make the method get the information.
At worst case, you can recursively loop through the Page.Controls and all
child controls and check if the control IS a button.

Karl
 
L

Lucas Tam

I have a group of buttons.
When the user clicks on a button I'd like to execute
same code. Every button should send its caption to that method.
How can I do that?

Just assign all buttons the same Event Handler.

Or change the Handles keyword to handle all the buttons.click
 
K

Ken Cox [Microsoft MVP]

Hi Mark,

You can have multiple events assigned to a common subroutine by listing the
events after the Handles clause

Private Sub MyButtonHandler _
(ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles Button1.Click, _
Button2.Click, Button3.Click

Ken
Microsoft MVP [ASP.NET]
 
D

David Jessee

side note:
Instead of checking for the button's "caption" (text) you might want to the
the CommandName property instead. Its not necessarily any better or worse,
but its a little more in alignment with other similar scenarios (e.g.
handling events for templated controls)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top