Generic button click event

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Is there a means of raising an event when ANY button on a windows application
form is clicked wherein the actual button clicked can be determined in the
generic click events code (eg by way of interogating the sender)?
 
mark said:
Is there a means of raising an event when ANY button on a windows
application
form is clicked wherein the actual button clicked can be determined in the
generic click events code (eg by way of interogating the sender)?

you could create a generic event and add all of the buttons to it in the
Handles clause.
 
The DirectCast statement simply converts the "sender" variable (which is
declared as an Object) to a Button. Essentially, if you click on Button1
you
get Button1 passed to the Click event handler as an object. Cast it back
to
a Button and you can do anything you want with it including accessing
it's
properties such as name, text, position, etc....

See this discussion for some more information. It's a very similar
problem... at least the part about adding multiple buttons...

http://www.devcity.net/forums/topic.asp?tid=4087&page=2

Remember in VB.NET you can have a single event handler handle multiple
events (as above) and you can have a single event handled by multiple
handlers...

Private Sub Stuff1(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
End Sub

Private Sub Stuff2(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MenuItem2.Click
End Sub


Private Sub Stuff3(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MenuItem2.Click,
Button1.Click

End Sub

In the above if you click Button1: Stuff1 & Stuff3 will execute, if you
click MenuItem2: Stuff2 & Stuff3 will execute.

In addition to the Handles keyword, you can use AddHandler &
RemoveHandler
to handle an event.
 

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

Back
Top