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.

GetRaiseMethod
zqzproject
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
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
Fredrik Skanberg
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