i need help on this simple case

dim xMenu as System.Windows.Forms.ToolstripItem
dim xString as string = "FrmMain.MnuFile"


xString.test="MENU FILE"

How could i do 'that bold area'??
note: xString actually was dynamic variable i got from Database with SQL syntax
thanks before!

6 Years
Discussion Span
Last Post by sartana

You want to build a form's menu dynamically from the database, am I right?

Here's an example how to create a menu dynamically. Menu creation happens when a button is clicked but should be done in some other way.

Private Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click

    ' Set up the initial menu
    Dim FormMenu As MenuStrip

    FormMenu = New MenuStrip()
    ' Show menu at the form's top
    FormMenu.Dock = DockStyle.Top
    ' Add menu to form's controls
    Me.MainMenuStrip = FormMenu ' Set also as a main menu
    Dim xMenu As System.Windows.Forms.ToolStripMenuItem
    Dim xString As String = "FrmMain.MnuFile" ' A dot separated value read from the DB (or file etc.)
    Dim Items() As String

    ' Get menu hierarchy
    Items = xString.Split(CChar("."))
    'Items(0) = "FrmMain"
    'Items(1) = "MnuFile" 

    ' Create an item for the menu
    xMenu = New ToolStripMenuItem()
    xMenu.Text = Items(1) ' Text to show
    xMenu.Name = Items(1) ' Name for this control
    xMenu.Owner = Me.MainMenuStrip
    ' Add an event handler for menu click
    AddHandler xMenu.Click, AddressOf onMenuClick
    ' Add to menu

  End Sub

  Private Sub onMenuClick(ByVal sender As Object, ByVal e As EventArgs)
    ' Menu event handler

    If CType(sender, ToolStripMenuItem).Name = "MnuFile" Then
      ' Notice: I had to hard code "MnuFile" in here
      MessageBox.Show("MnuFile selected", "Menu Event", MessageBoxButtons.OK, MessageBoxIcon.Information)
    End If

  End Sub

The important point is to remember add event handler for menu items' click event.



Thanks for your reply. It's good example to create dynamic menustrip.

Sorry before, maybe I didn't explain my needed clearly. Actually in my case, my form called "FrmMainMenu" already had a menustrip complete with it's item and sub-item, and sub-sub-item, and so on. Design base on table t_Module.
Table t_module Design :
ModulName Parent
MnuFile -
MnuChangePwd MnuFile
MnuExit MnuFile
MnuSales -
MnuDelivery MnuSales

MY GOAL : I want to set authority for user on Event FrmMainMenu_Load base on table t_Autority, so I can control which user can use (visible true) or cannot use (visible false) module in that MenuStripItem.
Table t_Authority Design :
UserName ModuleName View
Sartana MnuFile True
Sartana MnuChangePwd True
Sartana MnuExit True
Sartana MnuSales False
Sartana MnuDelivery False
Teme64 MnuFile True
Teme64 MnuChangePwd False
Teme64 MnuExit True
Teme64 MnuSales True
Teme64 MnuDelivery True

Before, i do logic like this
-create DataTable X, fill with query select * from t_Authority where UserName='xxx'
-looping from 0 to DataTable.Rows.Count-1
-set xString as string = DataTable.rows(i).item("ModuleName")
-set xValue as boolean = DataTable.rows(i).item("View")
-manipulate xString into an object, so i can do "xString.visible=xValue"
but I cannot do that because I don't know how to change STRING into OBJECT..

How to solve this problem in simple way? Or maybe in Some Other Way?
Sorry for being n00bs, Thank you before.


I see. Then you'll have to a) locate the menu control itself and b) locate the menu item from that menu hierarchy.

Here's an example

Private Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click

    ' Dim xString As String = "FrmMain.MnuFile" ' A dot separated value read from the DB (or file etc.)
    Dim xString As String = "MenuStrip1.mnuFile.mnuFileOpen" ' I used my naming convention, but still a menu naming hierarchy
    Dim xValue As String = "False" ' Allow/disallow (for _this_ user), value is either True or False
    Dim Items() As String

    ' Get menu hierarchy
    Items = xString.Split(CChar("."))
    'Items(0) = "MenuStrip1" ' "Root" menu control
    'Items(1) = "mnuFile" ' Menu item
    'Items(2) = "mnuFileNew" ' Submenu item

    ' Search the right menu item, first locate the menu itself
    For Each c As Control In Me.Controls()
      If c.Name = Items(0) Then ' Menu "root" control found ("MenuStrip1" in this case)
        For Each m As ToolStripDropDownItem In CType(c, ToolStrip).Items()
          If m.Name = Items(1) Then ' Top level menu item found ("mnuFile" in this case)
            For Each d As ToolStripMenuItem In m.DropDownItems()
              If d.Name = Items(2) Then ' Correct menu dropdown item in this menu item ("mnuFileOpen")
                d.Enabled = CBool(xValue) ' Set menu item state
              End If
          End If
      End If

  End Sub

And my menu (hierarchy) was:

File (control's name mnuFile)
- New (control's name mnuFileNew)
- Open (control's name mnuFileOpen) <- the item we're looking for
- Save (control's name mnuFileSave)

The menu (i.e. top level control in the form) was named MenuStrip1.

In your case you wouldn't put the code in a click event. Instead of that you would use the search loop above for each row in the table (for current user).

And one optimization: If you have a plenty of controls in the form, you could lookup the menu control (MenuStrip1) only once and then loop only for the correct ToolStripDropDownItem and the ToolStripMenuItem inside the menustrip.


This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.