K
Karl Rhodes
I'm building a windows forms app in VB.net 2005 and would like to know
if there is any way of adding a handler using a dynamic AddressOf
value?
The application will have a "Windows" menu item and I want to add a
list open forms as other menu items to this list. I want to be able to
do this dynamically and create a handler for each one that I add.
However, instead of creating the handler everytime i open a new form,
I want to use a single method that takes a couple of parameters and
adds the menu item and builds the handler. an example of what Id like
is shown below...
Private Sub AddItem(ByVal strMenuItem As String, ByVal strMenuItemName
As String)
If IsNothing(Me.menuWindows.DropDownItems.Item(strMenuItem)) Then
Dim oMI = New System.Windows.Forms.ToolStripMenuItem
Me.menuWindows.DropDownItems.AddRange(New
System.Windows.Forms.ToolStripItem() {oMI})
oMI.Name = strMenuItem
oMI.Text = strMenuItemName
AddHandler Me.menuWindows.DropDownItems(strMenuItem).Click,
AddressOf strMenuItem & "_Click"
End If
End Sub
Obviously I'd like to be able to remove menu items and handlers when
the forms are closed using a similar method. If anyone knows how I can
evalute 'strMenuItem & "_Click"' into a proper method name then I'd be
most grateful.
I thought that there would some some sort of Eval method that would do
it, and I'm sure there is something like it, but cant remember what it
is!
Thanks again
if there is any way of adding a handler using a dynamic AddressOf
value?
The application will have a "Windows" menu item and I want to add a
list open forms as other menu items to this list. I want to be able to
do this dynamically and create a handler for each one that I add.
However, instead of creating the handler everytime i open a new form,
I want to use a single method that takes a couple of parameters and
adds the menu item and builds the handler. an example of what Id like
is shown below...
Private Sub AddItem(ByVal strMenuItem As String, ByVal strMenuItemName
As String)
If IsNothing(Me.menuWindows.DropDownItems.Item(strMenuItem)) Then
Dim oMI = New System.Windows.Forms.ToolStripMenuItem
Me.menuWindows.DropDownItems.AddRange(New
System.Windows.Forms.ToolStripItem() {oMI})
oMI.Name = strMenuItem
oMI.Text = strMenuItemName
AddHandler Me.menuWindows.DropDownItems(strMenuItem).Click,
AddressOf strMenuItem & "_Click"
End If
End Sub
Obviously I'd like to be able to remove menu items and handlers when
the forms are closed using a similar method. If anyone knows how I can
evalute 'strMenuItem & "_Click"' into a proper method name then I'd be
most grateful.
I thought that there would some some sort of Eval method that would do
it, and I'm sure there is something like it, but cant remember what it
is!
Thanks again