CygNuS
Well-known member
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:
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
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: