Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Calling a procedure easily in 52 different buttons 1

Status
Not open for further replies.

daveonion

Programmer
Aug 21, 2002
359
GB
Hi I have 52 buttons (I know it sounds excessive), anyways on click of any of these buttons i want a procedure to be called, is there an easier way then going into the individual on click event of each button,

thanks for any help
 
after the sub add a handes and seperate all events by a comma..

eg Sub DoSomeStuff( agruments that match the event) handles button1.click, button1.click, button3.click etc......

HTH


Rob
 
An addition to NoCoolHandle's post: an interesting feature of .NET is that you can have multiple handlers for the same event, such as:

Sub DoSomeStuff( agruments that match the event) handles button1.click, button2.click, button3.click etc......


Sub DoSomeOtherStuff( agruments that match the event) handles button1.click, button2.click, button3.click etc......

Both of these will execute when any of the handled buttons' click events fire.

Personally, I think this is really cool. I haven't found a situation where I actually need to do this, but it's still cool. :D



I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson
 
Hey jebenson, when you have 2 subs that handle the same event, how is threading handled? are they both launched in the primary thread? if so, which one runs first?

-Rick

----------------------
[banghead]If you're about to post an ASP.Net question,
please don't do it in the VB.Net forum[banghead]

[monkey] I believe in killer coding ninja monkeys.[monkey]
 
The answer is that unless you code a new thread (which in this case you don't) it all runs on the main thread.

So just the same thread and the question is really, which will run first...:)

No time slicing, just pain old 1 thread where execution of one task must finish before it can start a new task.

Rob
 
You don't even have to manually add the "Handles btn1.click, btn2.click, <etc>" code for each one:

In a startup routine you can use AddHandler to bind the event for each button on the fly:

Public sub AddEventHandlers()
For Each ctrl As Control In Me.Controls
If TypeOf ctrl Is Button Then
AddHandler ctrl.click, AddressOf btn_Click
End If
Next
End Sub

Private Sub btn_Click(ByVal sender As Object, ByVal e As System.EventArgs)

...

End Sub
 
52 buttons... that's one button for every card in the deck! Is this a poker application? [afro]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top