Results 1 to 6 of 6

Thread: Handling An Event For A Collection Of Controls...

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2003
    Location
    UK
    Posts
    7

    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...

  2. #2
    Frenzied Member dynamic_sysop's Avatar
    Join Date
    Jun 2003
    Location
    Ashby, Leicestershire.
    Posts
    1,142
    here's a quick example...
    VB Code:
    1. [Color=Blue]Private[/color] [Color=Blue]WithEvents[/color] btn [Color=Blue]As[/color] Button
    2.  
    3.     [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
    4.         [Color=Blue]Dim[/color] ctl [Color=Blue]As[/color] Control
    5.         [Color=Blue]For[/color] [Color=Blue]Each[/color] ctl [Color=Blue]In[/color] [Color=Blue]Me[/color].Controls
    6.             [Color=Blue]If[/color] [Color=Blue]TypeOf[/color] ctl [Color=Blue]Is[/color] Button [Color=Blue]Then
    7. [/color]                [Color=Blue]AddHandler[/color] ctl.Click, [Color=Blue]AddressOf[/color] btn_Click
    8.             [Color=Blue]End[/color] [Color=Blue]If
    9. [/color]        [Color=Blue]Next
    10. [/color]    [Color=Blue]End[/color] [Color=Blue]Sub
    11.  
    12.  
    13. [/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
    14.         [Color=Blue]Dim[/color] b [Color=Blue]As[/color] Button = [Color=Blue]DirectCast[/color](sender, Button)
    15.         MessageBox.Show(b.Name)
    16.     [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]

  3. #3

    Thread Starter
    New Member
    Join Date
    Mar 2003
    Location
    UK
    Posts
    7
    I've just tried your example and it worked a treat!

    Thanks ever so much

  4. #4
    Hyperactive Member sw_is_great's Avatar
    Join Date
    Nov 2003
    Posts
    330
    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
    Regards

  5. #5
    Junior Member
    Join Date
    Oct 2003
    Posts
    26
    Does anyone knows how to achieve the same result using VBA??

    Thanks

  6. #6
    Frenzied Member
    Join Date
    Nov 2003
    Posts
    1,489
    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
  •  



Click Here to Expand Forum to Full Width