|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Menus from a database table and addhandlerI 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 *** Sent via Developersdex http://www.developersdex.com *** Andrew Wilkie wrote:
> Put the procedure name in the menu item's Tag property (this a general > 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 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. -- Larry Lard larryl***@googlemail.com The address is real, but unread - please reply to the group For VB and C# questions - tell us which version 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 *** Sent via Developersdex http://www.developersdex.com *** Andy wrote:
> I do mean procedure as in VB.NET procedure. I have looked at the Sorry for the delay.> 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 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. -- Larry Lard larryl***@googlemail.com The address is real, but unread - please reply to the group For VB and C# questions - tell us which version
array element isexist ?
How much disk space is needed... Deriving from Abstract Classes with Enums and Varying Object Types .Net and 32 processors Datagrids (again) System.OverflowException during IPostBackDataHandler.LoadPostData EDITITEMTEMPLATE FINDCONTROL PROBLEM Holding down keys II Error Compiling unhandled SecurityException in Visual Basic 2005 Express |
|||||||||||||||||||||||