GetRaiseMethod

I have a button on a form.  The click event is handled by a Procedure.  Is there any way to get the name of the procedure, using Reflection for instance:

Public Sub SaveData() handles btnSave.Click
     ' some code
end Sub.

I would like to be able to pass in btnSave as the control, and Click as the Event, and get back SaveData as the Method which is raised by this Event.

You help is greatly appreciated.


Answer this question

GetRaiseMethod

  • zqzproject

    If you happen to have an exception object just hanging around, that would do it. For example, if you were to raise an error and catch it, the <b><u>TargetSite.Name</u></b> would give you the method name.

    If you need to pass the name of the current method, you could pass this exception object and then retrieve the method name there.

    I know . . . this doesn't sound right since that's not what exception object were meant for but it works. Think of it like using a screwdriver to hammer a nail or pry something out of wood.  :) 

  • David C. Blake

    Please forgive me if I was not clear.

    By way of example: 

    Public Sub SaveData(sender as object, e as System.EventArgs) handles button1.click
         ValidateFields(sender)
         WriteToData
    End Sub

    Private Sub ValidateFields(sender as object)
         If FieldsAreValid then
              AddSaveHandler (sender)
         Else
              RemoveSaveHandler (sender)
         End if
    End Sub

    Private Sub AddSaveHandler(sender as object)
         AddHandler Sender.Click, AddressOf [Method which Handles Sender.Click]
    End Sub

    I would like to know which Method (i.e. SaveData) handles the click event of the sender. 

    Thus, for instance, if I have a number of different buttons each one being handled by a different Procedure, I will dynamically be able to determine which EventHandler to Add and which to Remove.  In this case, I do this to activate or cancel the Click Event, without having to add code in the Click event itself (i.e to enable a design time Extender object to the button to handle validations before allowing a save)


  • sign

    There are ways to get the "calling method" and on up the chain at runtime, but it sounds like that's not quite what you are looking for.  So, just to clarify, you want to take a control, take an event and [in theory] scan all code to see what raises the event
  • Fredrik Skanberg

    I see what you mean now and I've had to solve this same problem. I know that you want an elegant one-liner solution but I wasn't able to make it work that way. I hope someone reading this may have a better answer, in the mean time, here's a little sample I worked up using delegates. I'm no pro with delegates so I'm answering this time trying to further my knowledge a bit.

    First of all, in the AddSaveHandler method, use a Select statement based on the name of the 'sender'. With that, match the click event <u>method</u> that one you've <u>already written</u>. You might need more than one Select or If...Then statement (one embedded in another) if you are passing more than one object type.    Delegate Sub MyDelegate(ByVal sender As System.Object, ByVal e As System.EventArgs)

        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            AddSaveHandler(sender)
        End Sub

        Private Sub AddSaveHandler(ByVal sender As Object)
            Dim oButton As Button = sender
            Select Case oButton.Name
                Case "Button1"
                    Dim aDelegate As MyDelegate
                    aDelegate = New MyDelegate(AddressOf Button2_Click)
                    InvokeSomething(aDelegate)
                Case Else
                    ' Do something else.
            End Select
        End Sub

        Private Sub InvokeSomething(ByVal theDelegate As MyDelegate)
            Dim oTest As String = "Something I want to send."
            Dim e As System.EventArgs
            theDelegate.Invoke(oTest, e)
        End Sub

        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            Dim oTest As String = sender
            MessageBox.Show(oTest.ToString)
        End Sub

  • GetRaiseMethod