Results 1 to 9 of 9

Thread: [RESOLVED] AddHandler to multiple buttons

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    May 2017
    Location
    Italy
    Posts
    170

    Resolved [RESOLVED] AddHandler to multiple buttons

    my code is
    Code:
    Option Strict Off
    Public Class Form1
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            For Each tb As Control In Me.Controls
                If TypeOf tb Is Button Then AddHandler tb.Click, AddressOf BUTTON1Click
            Next
        End Sub
        Private Sub BUTTON1Click(ByVal sender As Object, ByVal e As System.EventArgs)
            MessageBox.Show(sender.name)
        End Sub
    End Class
    can you help me change the Off with On in the option ?

  2. #2
    Fanatic Member kpmc's Avatar
    Join Date
    Sep 2017
    Posts
    1,012

    Re: AddHandler to multiple buttons

    Code:
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            For Each Ctrl As Control In Controls
                If TypeOf Ctrl Is Button Then
                    AddHandler DirectCast(Ctrl, Button).Click, AddressOf HandleStuff
                End If
            Next
        End Sub
        Private Sub HandleStuff(sender As Object, e As EventArgs)
            'todo: Handle stuff
        End Sub
    Last edited by kpmc; Jun 4th, 2018 at 12:24 PM.

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    May 2017
    Location
    Italy
    Posts
    170

    Re: AddHandler to multiple buttons

    Thank You very much, I solved with
    Code:
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            For Each Ctrl As Control In Controls
                If TypeOf Ctrl Is Button Then
                    AddHandler Ctrl.Click, AddressOf BUTTON1Click
                End If
            Next
        End Sub
        Private Sub BUTTON1Click(ByVal sender As Object, ByVal e As System.EventArgs)
                MessageBox.Show(DirectCast(sender, Button).Name)
        End Sub

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,038

    Re: [RESOLVED] AddHandler to multiple buttons

    I was wondering about that, but didn't look it up. If Click is an event of control (it is), then you don't have to cast Ctrl to type Button, but had you used an event specific to button (I don't know whether there even is one), then you would have to cast. With option strict OFF, the compiler will add code to do the conversion. The code it adds won't be quite as quick as DirectCast, and if the conversion isn't possible, you get an exception, but the compiler is assuming that you know what you are doing and will add the code. It's better the way you are doing it, as it will be somewhat more efficient (though you'll never see the difference). Since .Name is a property of Control, you could also have written:

    MessageBox.Show(DirectCast(sender, Control).Name)

    There is no performance benefit of one over the other, except that by just casting to Control, that click event handler could be used to handle the click event of ANY control, not just buttons. That's usually of little benefit, as I'm sure you will be doing more than just showing the name of the control.
    My usual boring signature: Nothing

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    May 2017
    Location
    Italy
    Posts
    170

    Re: [RESOLVED] AddHandler to multiple buttons

    You are right Shaggy, i changed my code, thank You too

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

    Re: [RESOLVED] AddHandler to multiple buttons

    While it's a small thing, I would tend to change this:
    vb.net Code:
    1. For Each Ctrl As Control In Controls
    2.     If TypeOf Ctrl Is Button Then
    3.         AddHandler Ctrl.Click, AddressOf BUTTON1Click
    4.     End If
    5. Next
    to this:
    vb.net Code:
    1. For Each btn In Controls.OfType(Of Button)()
    2.     AddHandler btn.Click, AddressOf BUTTON1Click
    3. Next
    The OfType method filters and casts, so 'btn' is type Button.

  7. #7
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,038

    Re: [RESOLVED] AddHandler to multiple buttons

    I pondered those two for a bit, then decided to test them both. I couldn't decide whether I thought they'd perform the same or different. It doesn't matter, since in my test program I had to run each one 100,000 times to get the speed down to where you could actually see the difference, but the first one performs about 25% faster than the second. I was a bit surprised at that...but not very much.

    It's really a matter of preference. The second version takes less typing, and unless you are going to do that 100,000 times in a row, you won't see the difference.
    My usual boring signature: Nothing

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

    Re: [RESOLVED] AddHandler to multiple buttons

    Yeah, it's important to remember that LINQ will almost always perform worse than what you would usually write otherwise. I would expect only code that was at least relatively complex and possibly convoluted otherwise would actually improve performance-wise using LINQ. Remember that LINQ basically implements that more conventional code under the hood anyway, so you're generally adding another layer that the code has to execute. LINQ is generally about making code more succinct and that is generally a more pressing concern than performance when the difference will be milliseconds at most anyway.

  9. #9
    Fanatic Member Arve K.'s Avatar
    Join Date
    Sep 2008
    Location
    Kyrksæterøra, Norway
    Posts
    518

    Re: [RESOLVED] AddHandler to multiple buttons

    For what it's worth, the LINQ version looks better though.
    Arve K.

    Please mark your thread as resolved and add reputation to those who helped you solve your problem
    Disclaimer: I am not a professional programmer

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