Menus from a database table and addhandler

A

Andrew Wilkie

I have written code to create a menu on a VB form from a database table.
This all works fine. But what I need to do is associate the Handler
procedure for the click event of each menu. Each item could possibly
have a different event. The procedure name for each menu item is stored
in the table. This is read in to a text variable along with all the
other menu information. What I need to do is convert the procedure name
in the text variable so that it can be referenced by the AddHandler.

Any help will be appreciated
 
L

Larry Lard

Andrew said:
I have written code to create a menu on a VB form from a database table.
This all works fine. But what I need to do is associate the Handler
procedure for the click event of each menu. Each item could possibly
have a different event. The procedure name for each menu item is stored
in the table. This is read in to a text variable along with all the
other menu information. What I need to do is convert the procedure name
in the text variable so that it can be referenced by the AddHandler.

Any help will be appreciated

Put the procedure name in the menu item's Tag property (this a general
purpose Object property that is available for us to use for anything).
Wire up all the dynamically-created menu items to the same handler. In
this handler, examine sender.Tag for the procedure to call.

Or, if you mean 'procedure' as in VB.NET procedure (rather than database
stored procedure), you could even create appropriate delegates when you
create the menu items, and put the delegates in the Tag, to be called on
click.
 
A

Andy

I do mean procedure as in VB.NET procedure. I have looked at the
microsoft help on delegates and I am completely confused, and I was
wondering if you could possibly help further. The VB procedure name
that I need to run is currently read in to a string variable. I need to
associate this procedure name the menu handler.

Any help is appreciated as I have been at this for ages with no luck
 
L

Larry Lard

Andy said:
I do mean procedure as in VB.NET procedure. I have looked at the
microsoft help on delegates and I am completely confused, and I was
wondering if you could possibly help further. The VB procedure name
that I need to run is currently read in to a string variable. I need to
associate this procedure name the menu handler.

Any help is appreciated as I have been at this for ages with no luck

Sorry for the delay.

Just to recap: we're dynamically creating menu items, and for each one
the information we are given includes the name of a procedure that
should be called when the menu item is clicked.

The first thing to consider is: the handler procedure for a menu item's
Click event must be a standard EventHandler; that is, it must look like

Sub Whatever ( _
sender As Object, _
e As EventArgs _
)

Do all the procedures already have this signature? They'll need to in
order to avoid us having to do messy late binding stuff. So let's
suppose we have this code in the form:

Private Sub MyClickEventHandler(ByVal sender As Object, _
ByVal e As EventArgs)
MsgBox("bar!")
End Sub


Now, when we create each menu item, create a delegate to call the
procedure to be called on click, and put it in the .Tag property:

' for example
Dim menuItemText As String = "Bar"
Dim eventProc As String = "MyClickEventHandler"

' create the menu item
Dim menuItem As New MenuItem(menuItemText)
' set whatever other properties...

' create the event handler delegate
Dim del As [Delegate] = _
[Delegate].CreateDelegate(GetType(EventHandler), _
Me, _
eventProc)
' strongly type it
Dim clickEventHandler As EventHandler = _
CType(del, EventHandler)
' assign it to the click event
AddHandler menuItem.Click, clickEventHandler


I agree this looks confusing if you havne't done anything like this
before. Just remember that a delegate is a special type that
encapsulates a method's *signature* (its parameters and return type),
and instances of delegate types are objects to be created and
manipulated like any other.
 

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