|
-
Nov 26th, 2003, 03:51 PM
#1
Thread Starter
New Member
Handling An Event For A Collection Of Controls...
In VB6 you could handle the click event of a given group of controls such as Buttons and then do a Select Case on the passed in control index.
In VB.NET you can create a sub which Handles an event for a control, and if you need to handle multiple controls, then you add more controls to the list. Here lies the problem - is it possible to have one Handles keyword that handles the events for a collection of controls (of the same type) on a given form? For example, you have 5 buttons on a form, can one 'Handles' statement effectively handle each button click without you having to type in the name of each control?
Hope that made sense...
-
Nov 26th, 2003, 03:56 PM
#2
here's a quick example...
VB Code:
[Color=Blue]Private[/color] [Color=Blue]WithEvents[/color] btn [Color=Blue]As[/color] Button
[Color=Blue]Private[/color] [Color=Blue]Sub[/color] Form1_Load([Color=Blue]ByVal[/color] sender [Color=Blue]As[/color] System.Object, [Color=Blue]ByVal[/color] e [Color=Blue]As[/color] System.EventArgs) [Color=Blue]Handles[/color] [Color=Blue]MyBase[/color].Load
[Color=Blue]Dim[/color] ctl [Color=Blue]As[/color] Control
[Color=Blue]For[/color] [Color=Blue]Each[/color] ctl [Color=Blue]In[/color] [Color=Blue]Me[/color].Controls
[Color=Blue]If[/color] [Color=Blue]TypeOf[/color] ctl [Color=Blue]Is[/color] Button [Color=Blue]Then
[/color] [Color=Blue]AddHandler[/color] ctl.Click, [Color=Blue]AddressOf[/color] btn_Click
[Color=Blue]End[/color] [Color=Blue]If
[/color] [Color=Blue]Next
[/color] [Color=Blue]End[/color] [Color=Blue]Sub
[/color] [Color=Blue]Private[/color] [Color=Blue]Sub[/color] btn_Click([Color=Blue]ByVal[/color] sender [Color=Blue]As[/color] [Color=Blue]Object[/color], [Color=Blue]ByVal[/color] e [Color=Blue]As[/color] System.EventArgs) [Color=Blue]Handles[/color] btn.Click
[Color=Blue]Dim[/color] b [Color=Blue]As[/color] Button = [Color=Blue]DirectCast[/color](sender, Button)
MessageBox.Show(b.Name)
[Color=Blue]End[/color] [Color=Blue]Sub[/color]
hope it helps
~
if a post is resolved, please mark it as [Resolved]
protected string get_Signature(){return Censored;}
[vbcode][php] please use code tags when posting any code [/php][/vbcode]
-
Nov 26th, 2003, 05:13 PM
#3
Thread Starter
New Member
I've just tried your example and it worked a treat!
Thanks ever so much
-
Nov 27th, 2003, 07:20 AM
#4
Hyperactive Member
you can also do like :
Private Sub btn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btn1.Click,btn2.Click,btn3.Click.........................,btnn.Click
'your code
End Sub
-
Jan 1st, 2004, 09:07 PM
#5
Junior Member
Does anyone knows how to achieve the same result using VBA??
Thanks
-
Jan 1st, 2004, 10:30 PM
#6
Frenzied Member
Originally posted by sw_is_great
you can also do like :
Private Sub btn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btn1.Click,btn2.Click,btn3.Click.........................,btnn.Click
'your code
End Sub
Ibelieve this is more along the lines of what he was looking for. I think this a great feature of .net. It saves lots of time.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|