problem finding method by its name at runtime

CygNuS

Well-known member
Joined
Aug 21, 2006
Messages
89
Location
Belgium
Programming Experience
Beginner
problem finding method by its name at runtime - Solved

This is what i want to do (everything must be at runtime):

I have the name of a particular menuitem (in this case "MnuHLicentie")
Now I want to create a new toolbarbutton which has the same click-functionality as that menuitem.

Sounds easy doesn't it? Well, here's my code:

VB.NET:
Private Sub LoadCustomToolBar()
 
Dim menuItemToFind As String = "MnuHLicentie" 'value will come out of database later on
Dim foundObject As Object = FindControl("MnuHLicentie")
Dim menuItemToUse As ToolStripMenuItem = CType(foundObject, ToolStripMenuItem)
 
Dim newButton As New ToolStripButton
newButton.Name = "Toolbar" & menuItemToFind
newButton.Image = Image.FromFile("images/img1.ico")
newButton.Text = "Licentie"
newButton.DisplayStyle = ToolStripItemDisplayStyle.ImageAndText
newButton.TextImageRelation = TextImageRelation.ImageAboveText
 
AddHandler newButton.Click, AddressOf menuItemToFind & "_Click"
 
CustomToolBar.Items.Add(newButton)
 
End Sub
 
 
Private Sub MnuHLicentie_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MnuHLicentie.Click
 
Dim frm_Licentie As New Licentie
frm_Licentie.MdiParent = Me
frm_Licentie.Show()
 
End Sub
 
 
Protected Overridable Function FindControl(ByVal Name As String) As Object
 
Dim propInfo As Reflection.PropertyInfo
propInfo = Me.GetType().GetProperty(Name, Reflection.BindingFlags.IgnoreCase Or _
Reflection.BindingFlags.Instance Or Reflection.BindingFlags.NonPublic Or Reflection.BindingFlags.Public)
 
If Not propInfo Is Nothing Then
Dim value As Object = propInfo.GetValue(Me, Nothing)
Return value
End If
Return Nothing
 
End Function

This line is my problem:
AddHandler newButton.Click, AddressOf menuItemToFind & "_Click"

This doesn't work of course, it must be the name of a method instead of a string.

Is there a way for me to find the method with name (menuItemToFind & "_Click") at runtime, so i can put it after the addressof clause?

I suspect this will have something to do with reflection but i don't seem to find it.

Thanks in advance
 
Last edited:
There is a way.... I'd have to look it up though....

-tg
 
YES i found out myself!

It's not exactly coded the way i wanted, but it's a workaround.
I did it like this:

In addition to my code in my first post i added these lines:

VB.NET:
in Sub LoadCustomToolbar:
...
newButton.Tag = menuItemToFind
AddHandler newButton.Click, AddressOf CustomToolButtonClickHandler
...
 
Private Sub CustomToolButtonClickHandler(ByVal sender As System.Object, ByVal e As System.EventArgs)
 
'Get the tag of the button that has been clicked 
'-> tag contains name of the menuitem that this button is based on
Dim menuItemNameOnWhichBased As String = sender.tag
 
'Get the functionality of the click event of that menuitem
methinfo = FindMethod(menuItemNameOnWhichBased & "_Click")
 
'Start this method
methinfo.Invoke(Me, New Object() {sender, e})
End Sub
 
Protected Overridable Function FindMethod(ByVal Name As String) As MethodInfo
 
Dim methInfo As Reflection.MethodInfo
methInfo = Me.GetType().GetMethod(Name, Reflection.BindingFlags.IgnoreCase Or _
Reflection.BindingFlags.Instance Or Reflection.BindingFlags.NonPublic Or Reflection.BindingFlags.Public)
If Not methInfo Is Nothing Then
Return methInfo
End If
 
Return Nothing
End Function

Basically i wrote a CustomToolButtonClickHandler which is called everytime any of the buttons is clicked and in there i invoke the wanted menuitem_click method by using reflection.

Granted, this is LOTS of code for what i wanted to achieve in my first post. It sounded easier than it was.

Hope this helps anyone stuck with a similar problem!
 
Last edited:
I'd be inclined to do things quite differently. I would create a Dictionary(Of ToolStripButton, ToolStripMenuItem) and then once you have a reference to the menu item you would create the button and add them as an item to the dictionary. You can then use a single event handler for the Click event of all the buttons. You then get the corresponding menu item from the dictionary and call its PerformClick method.
 
I'd be inclined to do things quite differently. I would create a Dictionary(Of ToolStripButton, ToolStripMenuItem) and then once you have a reference to the menu item you would create the button and add them as an item to the dictionary. You can then use a single event handler for the Click event of all the buttons. You then get the corresponding menu item from the dictionary and call its PerformClick method.

Thx for the alternative! I was unaware that there was such a method PerformClick. I'll check out the use of Dictionaries too in the future for similar problems.

For now i'll leave my code be :cool:
 
Back
Top