Dynamic add event to ToolStripMenuItem VB2005

Biodrome

New member
Joined
Feb 16, 2010
Messages
1
Programming Experience
1-3
I hope you can help me with the following question:

I have the next table where i store the menuitem info.

In the onAcion field I stored the info about the event that must be called when the menuitem clicked. (In ms Access i used the Commandbar..OnAction = rst("MeOnAction") methode)

VB.NET:
SELECT [MeShortcutMenuBarDetailsID]
      ,[MeShortcutMenuBarId]
      ,[MeCaption]
      ,[MeOnAction]
      ,[MeBeginGroup]
      ,[MeFaceId]
  FROM [DesignMaster].[dbo].[MeShortcutMenuBarDetails]


So far so good and i used the data in the next sub

VB.NET:
Public Sub Create_ContextMenu(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)

        Dim contextMenuStrip As ContextMenuStrip
        Dim firstItem As ToolStripMenuItem
        'Dim subItem As ToolStripMenuItem
        Dim oconn As New SBConnector.ADODBTools
        Dim oHandler As New _EventBuilder

        contextMenuStrip = New ContextMenuStrip()
        Dim rdr As SqlDataReader = oconn.GetReader("SELECT S.MeShortcutMenuBarName, I.MeCaption, I.MeOnAction, I.MeBeginGroup, I.MeFaceId" & _
                " FROM dbo.MeShortcutMenuBar AS S INNER JOIN" & _
                " dbo.MeShortcutMenuBarDetails AS I ON S.MeShortcutMenuBarID = I.MeShortcutMenuBarId" & _
                " WHERE S.MeShortcutMenuBarName = N'" & ContextName & "'")

        While rdr.Read()
            If IsDBNull(CType(rdr.Item("MeCaption"), String)) Then
                Exit While
            End If



            firstItem = New ToolStripMenuItem(CType(rdr.Item("MeCaption"), String), Nothing, New System.EventHandler(AddressOf Me.Handle_ContextMenuClick))

            If Not IsDBNull(rdr.Item("MeOnAction")) Then firstItem.Tag = CType(rdr.Item("MeOnAction"), String)

            contextMenuStrip.Items.Add(firstItem)
        End While

        contextMenuStrip.Show(sender, e.X, e.Y)
        
    End Sub

My question is if it is posible to add dynamic the OnAction string as a eventhandler. Your awnser is welcome :)
 
So you're saying that the MeOnAction column would contain the name of a method that you want to handle the menu item's Click event, correct? If so then it could be done but it would be a bit convoluted and it's a bad idea anyway. How many possible event handlers could there be? Why not just use a number to identify each one and then use a Select Case statement to choose the appropriate one?
 
Back
Top