Dynamic AddressOf value for AddHandler

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
 
A

Armin Zingler

Karl Rhodes said:
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

Do you have to have one event handler per menu item? You could also
handle them all in one procedure. The 'sender' arg points to, guess
what, the sender of the event, so you can still distingiush and keep the
event hooking simple.

Otherwise, you could use Reflection (see Framework docs TOC) to resolve
the procedure name at runtime. However, I'm not a friend of this
approach as long as there are straighter ways to do the same. Reflection
creates the potential risk of finding an error at runtime, which means
not ASAP, for the compiler isn't able to do the check. In addition, it's
(a little) slower at runtime. Doing this just for writing fewer source
code chars, should be avoided, IMO.

So, I suggest:

Private Sub AddItem( _
ByVal strMenuItem As String, _
ByVal strMenuItemName As String, _
byval Handler as EventHandler)

'...
AddHandler Me.menuWindows.DropDownItems(strMenuItem).Click, Handler
'...

End Sub

Call:
AddItem "bla","blub", addressof bla_Click



Let me please add: How much worth is this Sub? Do you really have to
check for Nothing? BTW, the 'Is' operator (Is Nothing) is more
straightforward than calling a function that does the same. Why do you
create an array of ToolStripItems for one single item? Do you really
need to set the name property? You could use one of the overloaded
constructors of the ToolStripMenuItem class, and, starting with VB
2008(?), you can use the With clause to initialize properties. Just a
thought.


AZ
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top