Results 1 to 17 of 17

Thread: [RESOLVED] [2005] Handle Groups?

  1. #1

    Thread Starter
    Fanatic Member Vectris's Avatar
    Join Date
    Dec 2008
    Location
    USA
    Posts
    941

    Resolved [RESOLVED] [2005] Handle Groups?

    I was just wondering if their was an easy way to create one handle for several controls.

    For instance, I have 20 buttons and I want them all to do the same thing when clicked. Instead of having 20 handles for each button.click, is there a way to create one handle that can cover them all, such as a grouping or something?

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: [2005] Handle Groups?

    You just put them all in the Handles clause of the event handler. You can do it manually or have the IDE do it for you. To do that you select all the controls in the designer, open the Properties window, click the Events button and then double-click the event of interest.

  3. #3
    PowerPoster Deepak Sakpal's Avatar
    Join Date
    Mar 2002
    Location
    Mumbai, India
    Posts
    2,424

    Re: [2005] Handle Groups?

    That is pretty easy.

    If you have dropped the buttons on form at design time then use this code:

    vb.net Code:
    1. Private Sub Button_Click( _
    2.     ByVal sender As System.Object, _
    3.     ByVal e As System.EventArgs _
    4. ) Handles Button1.Click, Button2.Click, Button3.Click, Button4.Click
    5.  
    6.     ' do stufff
    7. End Sub

    And if you are creating controls at run-time then use below code:

    vb.net Code:
    1. AddHandler Button1.Click, AddressOf Button_Click
    2. AddHandler Button2.Click, AddressOf Button_Click
    3. AddHandler Button3.Click, AddressOf Button_Click
    4. AddHandler Button4.Click, AddressOf Button_Click

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: [2005] Handle Groups?

    Just to clarify, if you were to follow the instructions I provided then you'd end up with code much like what DS provided in his first code snippet. If you need to know whcih Button was actually clicked then you can get a reference to it from the 'sender' parameter.

  5. #5

    Thread Starter
    Fanatic Member Vectris's Avatar
    Join Date
    Dec 2008
    Location
    USA
    Posts
    941

    Re: [2005] Handle Groups?

    Thanks, that didn't really solve the problem that I addressed but jmc your first suggestion will work for now.

    I've used multiple events in the handles for a while, so I know how that works. What I want to know is if there is a way to create one variable or something for a group of buttons, and then I can use that variable.click in the handles section of a procedure and it will cover them all.

    I don't want to have to type in "Handles button1.click button2.click button3.click...".

    All the way to 20 or however many I use.

    jmc your first suggestion may work in that I don't have to do as much typing if I simply select and then use the properties window to assign them all to an event automatically.

    Thank you both for the suggestions though.

  6. #6
    Frenzied Member Bulldog's Avatar
    Join Date
    Jun 2005
    Location
    South UK
    Posts
    1,950

    Re: [2005] Handle Groups?

    If you have a lot of buttons, make them into an array of buttons, then create the AddHandler (per Deepak's post) in a simple loop.


    • If my post helped you, please Rate it
    • If your problem is solved please also mark the thread resolved

    I use VS2015 (unless otherwise stated).
    _________________________________________________________________________________
    B.Sc(Hons), AUS.P, C.Eng, MIET, MIEEE, MBCS / MCSE+Sec, MCSA+Sec, MCP, A+, Net+, Sec+, MCIWD, CIWP, CIWA
    I wrote my very first program in 1979, using machine code on a mechanical Olivetti teletype connected to an 8-bit, 78 instruction, 1MHz, Motorola 6800 multi-user system with 2k of memory. Using Windows, I dont think my situation has improved.

  7. #7

    Thread Starter
    Fanatic Member Vectris's Avatar
    Join Date
    Dec 2008
    Location
    USA
    Posts
    941

    Re: [2005] Handle Groups?

    How can I make an array of buttons?

  8. #8
    Frenzied Member Bulldog's Avatar
    Join Date
    Jun 2005
    Location
    South UK
    Posts
    1,950

    Re: [2005] Handle Groups?

    Dim MyButtons() As Button

    or you can create the array by clustering your existing buttons;

    Dim MyButtons(4) As Button = {MyButton1, MyButton2, ExitButto, SaveButton}

    Controls can also be grouped into an array programmatically, so you do not need to type all of them in, this can be done by looping through Me.Controls, and adding any occurrences of a button control to an array.

    Arrays such as this allow you to loop through controls, rather than consulting each control with individual statements.


    • If my post helped you, please Rate it
    • If your problem is solved please also mark the thread resolved

    I use VS2015 (unless otherwise stated).
    _________________________________________________________________________________
    B.Sc(Hons), AUS.P, C.Eng, MIET, MIEEE, MBCS / MCSE+Sec, MCSA+Sec, MCP, A+, Net+, Sec+, MCIWD, CIWP, CIWA
    I wrote my very first program in 1979, using machine code on a mechanical Olivetti teletype connected to an 8-bit, 78 instruction, 1MHz, Motorola 6800 multi-user system with 2k of memory. Using Windows, I dont think my situation has improved.

  9. #9

    Thread Starter
    Fanatic Member Vectris's Avatar
    Join Date
    Dec 2008
    Location
    USA
    Posts
    941

    Re: [2005] Handle Groups?

    Ok so once I put the names of the button in the array, I can just use:

    Handles MyButtons.Click

    To have the click event for every button in the array all in one handle? I'll try it out.

    Also, is there a specific command for looping through arrays? I know what you mean by looping through me.controls using "for each ctrl as control in controls", but is there something for arrays like "for each variable as [slot] in array"?

  10. #10
    Frenzied Member Bulldog's Avatar
    Join Date
    Jun 2005
    Location
    South UK
    Posts
    1,950

    Re: [2005] Handle Groups?

    ok, let's be more succinct here.

    If you have an array of buttons called MyButtons, you can loop through using loops such as;
    Code:
    For Each B As Button In MyButtons
        B.Enabled = False
    Next
    So using a loop like that, you can assign the same event handler to every button and have one sub that will be called when any of those buttons is clicked. Within that sub, you can determine which button has been clicked by virtue of the "sender" property.

    To loop through all controls, you will need to determine if a given control is the type you want. So the loop below will show the names of all buttons on "Me."
    Code:
            For Each Con As Control In Me.Controls
                If Con.GetType.ToString = "System.Windows.Forms.Button" Then
                    MessageBox.Show(Con.Name & " is a button")
                End If
            Next
    You do need care in a loop such as this to check the component type. If you did not do that and tried to change a button property, but the current control was actually a CheckBox for example, you will get an exception thrown (if the CheckBox does not have that same property).
    Last edited by Bulldog; Feb 13th, 2009 at 08:31 PM.


    • If my post helped you, please Rate it
    • If your problem is solved please also mark the thread resolved

    I use VS2015 (unless otherwise stated).
    _________________________________________________________________________________
    B.Sc(Hons), AUS.P, C.Eng, MIET, MIEEE, MBCS / MCSE+Sec, MCSA+Sec, MCP, A+, Net+, Sec+, MCIWD, CIWP, CIWA
    I wrote my very first program in 1979, using machine code on a mechanical Olivetti teletype connected to an 8-bit, 78 instruction, 1MHz, Motorola 6800 multi-user system with 2k of memory. Using Windows, I dont think my situation has improved.

  11. #11
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: [2005] Handle Groups?

    Quote Originally Posted by Vectris
    Ok so once I put the names of the button in the array, I can just use:

    Handles MyButtons.Click

    To have the click event for every button in the array all in one handle? I'll try it out.

    Also, is there a specific command for looping through arrays? I know what you mean by looping through me.controls using "for each ctrl as control in controls", but is there something for arrays like "for each variable as [slot] in array"?
    No you have misunderstood. An array is an array. It has no Click event. If you want to handle the Click events of multiple Buttons then you have to handle the Click events of multiple Buttons, plain and simple. You either have to put them all in the Handles clause or else you have to call AddHandler for them all. What you're suggesting was possible in VB6 with control arrays but is not in VB.NET

    Basically, this a waste of time. You're writing code to save yourself writing code but, as I've demonstrated, you don't have to write the code you're trying to avoid anyway. Just use the designer to create and/or attach your event handlers, which is exactly why VB.NET doesn't support control arrays in the first place.

  12. #12

    Thread Starter
    Fanatic Member Vectris's Avatar
    Join Date
    Dec 2008
    Location
    USA
    Posts
    941

    Re: [2005] Handle Groups?

    Quote Originally Posted by jmcilhinney
    No you have misunderstood. An array is an array. It has no Click event.
    Well I know arrays don't have click events but I was wondering if adding click to the end of a special kind of array for controls or something would automatically read the contents and handle the click events for all the controls in the array.

    Basically, this a waste of time.
    Well I don't see this as a waste of time, I see it as a way to save time, that's why I'm asking.

    As for the rest of your post, is that just a "no" that it' can't be done? I'm not used to using the designer to make handles, I just go to the view code area and select the control and then the event. Then I add in handles as I need them. I was just looking for a way to add a bunch of handles quickly or cover a bunch of handles with just one.

  13. #13
    Frenzied Member MaximilianMayrhofer's Avatar
    Join Date
    Aug 2007
    Location
    IM IN YR LOOP
    Posts
    2,001

    Re: [2005] Handle Groups?

    Fact of the matter is, if you put a whole bunch of buttons on your form, then you're going to need a whole bunch of handlers. The benefit of tying all buttons to a single handler is that much less code needs to be written, making your program leaner and more efficient.

    But whining about the correct solution because you may have to do a little typing is just childish and pointless. In the time it took you to post complaints, you could have finished adding the required code and done something useful.

  14. #14
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Re: [2005] Handle Groups?

    I always think writing the code to do the loops is much more efficient in the long run. I have found myself adding 80 event handlers to buttons using the designer, and then having to delete about 20, and add another 30, etc...
    Using a loop you don't need to make any adjustments to your code after adding or removing buttons later.

    First, create a List(Of Button) to hold your buttons. In the Form_Load loop through all the controls on the form and add only the buttons to this List.
    In addition, you add an event handler to each button.
    vb.net Code:
    1. Dim buttons As List(Of Button)
    2.  
    3. Private Sub Form1_Load(...) Handles Mybase.Load
    4.    
    5.    For Each ctrl As Control In Me.Controls
    6.       If TypeOf ctrl Is Button Then
    7.          Dim btn As Button = DirectCast(ctrl,Button)
    8.          buttons.Add(btn)
    9.          AddHandler buttons_Click, AddressOf btn.Click
    10.       End If
    11.    Next
    12.  
    13. End Sub
    14.  
    15. Private Sub buttons_Click(sender As Object, e As EventArgs)
    16.    Dim btn As Button = DirectCast(sender, Button)
    17.  
    18.    btn.Text = "Hi, I'm a clicked button!"
    19. End Sub

    Whenever you might need to loop through all the buttons now you can loop through the List:
    vb.net Code:
    1. For Each btn As Button In buttons
    2.    btn....
    3. Next

  15. #15
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: [2005] Handle Groups?

    You might be looking for something like this:
    vb.net Code:
    1. Dim buttons() As Button
    2. Dim textBoxes() As TextBox
    3.  
    4. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    5.     'initialize array of controls we want to use
    6.     buttons = New Button() {Button2, Button3, Button4, Button5}
    7.     textBoxes = New TextBox() {TextBox1, TextBox2, TextBox3, TextBox4, TextBox5}
    8.  
    9.     'Attach whatever events you want to. Ensure correct spelling :)
    10.     AttachEvent("Click", AddressOf Button_Click, buttons)
    11.     AttachEvent("TextChanged", AddressOf TextBox_TextChanged, textBoxes)
    12. End Sub
    13.  
    14. Private Sub AttachEvent(ByVal eventName As String, ByVal eventHandler As EventHandler, ByVal ParamArray ctrls() As Control)
    15.     Dim e As Reflection.EventInfo = ctrls(0).GetType.GetEvent(eventName)
    16.     For Each ctrl In ctrls
    17.         e.AddEventHandler(ctrl, eventHandler)
    18.     Next
    19. End Sub
    20.  
    21. Private Sub Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
    22.     'button click event code here
    23.     MsgBox(sender.Name & " clicked")
    24. End Sub
    25.  
    26. Private Sub TextBox_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs)
    27.     'text-change event code here
    28.     MsgBox(sender.Name & " text-changed")
    29. End Sub

    The code is self-explanatory. Let me know if you need some clarifications.

    Pradeep
    Last edited by Pradeep1210; Feb 14th, 2009 at 05:27 AM.
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  16. #16
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: [2005] Handle Groups?

    Quote Originally Posted by Vectris
    Well I know arrays don't have click events but I was wondering if adding click to the end of a special kind of array for controls or something would automatically read the contents and handle the click events for all the controls in the array.
    There is no special kind of array. There was in VB6. There isn't in VB.NET.
    Quote Originally Posted by Vectris
    Well I don't see this as a waste of time, I see it as a way to save time, that's why I'm asking.

    As for the rest of your post, is that just a "no" that it' can't be done? I'm not used to using the designer to make handles, I just go to the view code area and select the control and then the event. Then I add in handles as I need them. I was just looking for a way to add a bunch of handles quickly or cover a bunch of handles with just one.
    All this talk of writing code to loop through all the buttons top attach event handlers is a waste of time. You asked for a quick way to add a control to a Handles clause and you have one: use the Properties window of the designer.

  17. #17

    Thread Starter
    Fanatic Member Vectris's Avatar
    Join Date
    Dec 2008
    Location
    USA
    Posts
    941

    Re: [2005] Handle Groups?

    Quote Originally Posted by MaximilianMayrhofer
    But whining about the correct solution because you may have to do a little typing is just childish and pointless.
    I'm not whining, I asked a question and I want my question answered, that's why I asked it. I don't want a bunch of "no you can't do this", just don't answer it then. Also I fail to see any "correct solutions" other than using the properties window, and I'm not complaining about that at all, in fact that's what I'm going to use, and it doesn't involve typing.

    @Nick

    While the properties window should be faster that will work very well for adding handles during runtime or just as an alternate way. I also like having a list of buttons, I never thought of doing that but it can be useful, thanks.

    All this talk of writing code to loop through all the buttons top attach event handlers is a waste of time.
    It's not a waste of time if I need to re-asign lots of handles during runtime . But properties window will most likely be what I use for now.

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