none
How can I know which control call the procedure when I use AddHandler statement? RRS feed

  • Question

  • If I write something like :

    AddHandler Button1.Click, AddressOf Button_Click
    AddHandler Button2.Click, AddressOf Button_Click
    AddHandler Button3.Click, AddressOf Button_Click

    How can I know which Button call (Button_Click)?

    And Thanks.

    Friday, May 5, 2017 12:19 PM

All replies

  • How can I know which Button call (Button_Click)?

    The sender argument passed to the Button_Click method is the control that raised the event.  Usually, that's all you need to know - just use the sender variable as a reference to the control.  If you need more specific information about the button then you can save it in the Tag property of the control so it can be retrieved in the event handler.   The sender argument in the method signature is probably declared as type Object, so you will usually cast it to the correct type (eg, Button) if you need to access other properties of the control.  See:
    https://msdn.microsoft.com/en-us/library/4x2877xb(v=vs.140).aspx
    Friday, May 5, 2017 12:38 PM
  • Hi

    Here is an example that may help.

    ' Form1 with Button1, Button2, Button3 and Label1
    Option Strict On
    Option Explicit On
    Public Class Form1
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            AddHandler Button1.Click, AddressOf Button_Click
            AddHandler Button2.Click, AddressOf Button_Click
            AddHandler Button3.Click, AddressOf Button_Click
        End Sub
        Private Sub Button_Click(sender As Object, e As EventArgs)
            Dim b As Button = DirectCast(sender, Button)
            Label1.Text = b.Name & " Clicked"
        End Sub
    
    End Class


    Regards Les, Livingston, Scotland


    Friday, May 5, 2017 12:43 PM
  • Be aware that VB (from version 7 which others call Net) has unlike C# an extra way to add handlers. 

    Regrettable enough they have removed in version 13, 14 and 15 the easy way of VB to add methods including handlers by using the comboboxes above the code.

    Nevertheless you can use it.Simply write behind the code of the Sub the words Handles, then intelisence will lead you to the easy way

    You see this in the code of Les in the Load method.

    By the way, there have been petitions from C# developers to get parity about this with VB, maybe is that the reason they removed the combobox way in VB. (But lucky enough the handles clause because then we got a breaking change)

    You can see this easy way in the rest of this thread.

    Success
    Cor


    Friday, May 5, 2017 12:53 PM
  • ...

    Regrettable enough they have removed in version 13, 14 and 15 the easy way of VB to add methods including handlers by using the comboboxes above the code.

    ....

    Cor, I'm not sure what you mean because this still works. You can select a control in the center combobox and an event in the right-most combobox (VS2015/2017) and the code editor will generate a handler method. In VS2013 and prior it is just the left and right comboboxes (there is no middle).

    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Friday, May 5, 2017 1:08 PM
    Moderator

  • Cor, I'm not sure what you mean because this still works. You can select a control in the center combobox and an event in the right-most combobox (VS2015/2017) and the code editor will generate a handler method. In VS2013 and prior it is just the left and right comboboxes (there is no middle).

    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Reed,

    Then maybe you can help me (I got never any reaction also not from somebody from the team)

    This is what I see in version 2012

    This is what I see in version 2017

    I am suspicious because this was also done in a very first beta version of VB8. The team did not find it important but got all MVP's over them so it was quickly changed. I hate that C# way inside the properties, that takes much more time. 

    But maybe it is just a setting which I could not find. If you know it, you are even more my man. 

     :-)


    Success
    Cor

    Friday, May 5, 2017 2:04 PM
  • Thanks Acamar that was very useful. :)
    Friday, May 5, 2017 2:28 PM

  • Cor, I'm not sure what you mean because this still works. You can select a control in the center combobox and an event in the right-most combobox (VS2015/2017) and the code editor will generate a handler method. In VS2013 and prior it is just the left and right comboboxes (there is no middle).

    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Reed,

    Then maybe you can help me (I got never any reaction also not from somebody from the team)

    This is what I see in version 2012

    This is what I see in version 2017

    I am suspicious because this was also done in a very first beta version of VB8. The team did not find it important but got all MVP's over them so it was quickly changed. I hate that C# way inside the properties, that takes much more time. 

    But maybe it is just a setting which I could not find. If you know it, you are even more my man. 

     :-)


    Success
    Cor

    Hi Cor

    I found same here when switching to 2017. I then found a setting that seems to be helpful:

    Options->Text Editor->General->Settings-Navigation Bar

    selecting this option may be what you want. Maybe?


    Regards Les, Livingston, Scotland


    • Edited by leshay Friday, May 5, 2017 3:09 PM typo
    Friday, May 5, 2017 2:29 PM
  • If you want to invoke code e.g. call a function etc.

    Public Class Form1
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            AddHandler Button1.Click, AddressOf ButtonClick
            AddHandler Button2.Click, AddressOf ButtonClick
            AddHandler Button3.Click, AddressOf ButtonClick
        End Sub
        Private Sub ButtonClick(sender As Object, e As EventArgs)
            Select Case CType(sender, Button).Name
                Case "Button1"
                    CallByName(Me, "Button1Click", CallType.Method)
                Case "Button2"
                    MessageBox.Show("Button2 code")
                Case "Button3"
                    MessageBox.Show("Button3 code")
            End Select
        End Sub
        Public Sub Button1Click()
            MessageBox.Show("1")
        End Sub
    End Class
    


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Friday, May 5, 2017 2:51 PM
    Moderator
  • @Les,

    You are for me the Man. 

    It is not in General, it was in specific Basic

    @Reed,

    At least I find this something for a sticky, this was not in version 2012 (I don't have version 2013 and 2015 anymore installed and there it was the same as I've shown in the previous message, I checked a lot but never found this, will you verify that because I'm curious about that.)


    Success
    Cor



    Friday, May 5, 2017 3:27 PM
  • @Les,

    You are for me the Man. 

    It is not in General, it was in specific Basic




    Hi

    Yes, my mistake. I meant to post:

    Options->Text Editor->Basic->General->Settings-Navigation Bar

    Of course, just going as far as Basic gets there anyway.


    Regards Les, Livingston, Scotland


    • Edited by leshay Friday, May 5, 2017 3:39 PM
    Friday, May 5, 2017 3:36 PM
  • Or Karens code with the "Handles" clause

        Private Sub ButtonClick(sender As Object, e As EventArgs) Handles Button1.Click, Button2.Click, Button3.Click
            Select Case DirectCast(sender, Button).Name
                Case "Button1"
                    CallByName(Me, "Button1Click", CallType.Method)
                Case "Button2"
                    MessageBox.Show("Button2 code")
                Case "Button3"
                    MessageBox.Show("Button3 code")
            End Select
        End Sub


    Success
    Cor

    Friday, May 5, 2017 4:01 PM
  • @Cor,

    Sorry I missed the reply, but it looks like Les got you straightened out.  :)

    Here are the options screens for

    VS2015

    And VS2013:


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Friday, May 5, 2017 4:24 PM
    Moderator
  • @Reed,


    Thanks,


    Cor


    Friday, May 5, 2017 4:27 PM
  • Hi ProgX.net,

    Please remember to close your thread by marking helpful post as answer, it is very beneficial to the other who face the same issue.

    Thanks for your understanding.

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, May 12, 2017 5:50 AM
    Moderator