Click here to Skip to main content
14,872,052 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to create menustrip dynamically in which will read information from sqlserver tables , as per right it will create the menu. please help if some sample solution forward to on following email address [EDIT - email removed] thank you.
Updated 11-Apr-21 15:00pm
CHill60 5-Jan-14 10:00am
I've removed your email address from the post - spam bots can pick this up and bombard your email address with rubbish. It's also not how things are done on this forum - answers are posted here for the benefit of all.
What have you tried so far and where are you stuck? Are you saying that the menu items will be read from a database at run time?

Someone did something similar, here at Code Project: "Dynamic Creation Of MenuStrip - VB.NET"[^].
Espen Harlinn 5-Jan-14 11:27am
Nice link :-)
Member 10503568 5-Jan-14 15:09pm
I tried the attached codes, its giving me problem if someone has solution I will be thankful
CHill60 5-Jan-14 16:12pm
I've given an alternative solution
Maciej Los 5-Jan-14 16:44pm
As the OP has tried Solution 1 and is having some problems here's my twopennorth...

I created a Windows Forms application in VB.NET where the only control on the Form is an "empty" MenuStrip - i.e. no menu items added via the Designer

I decided to hold my Menu definitions in an XML document ... the style of XML lends itself nicely to a menu structure and an XML document can be populated in a variety of ways. For the purposes of this example I just stored the data in a text document which I just load
Private Function GetMenus() As XmlDocument
    'This would be replaced with extracting the data from a database or similar
    'For demo/testing I'm just loading a fixed xml document
    Dim xxml As XmlDocument = New XmlDocument()
    Return xxml
End Function

This is the XML I used - I hope I've explained it well enough
<?xml version="1.0" encoding="UTF-8"?>
layout here is TopLevel node has attribute 'text' which is the text for that menu item
Any sub-menus are in the NextLevel nodes which also have the 'text' attribute
NextLevel has InnerText set to the name of the function to call
If there are no NextLevel Nodes then the TopLevel node has InnerText set to the name of the function to call
  <TopLevel text="Menu 1">
    <NextLevel text="Menu 1.A">Menu1A</NextLevel>
    <NextLevel text="Menu 1.B">Menu1B</NextLevel>
    <NextLevel text="Menu 1.C">Menu1C</NextLevel>
  <TopLevel text="Menu 2">Menu2</TopLevel>
  <TopLevel text="Menu 3">
    <NextLevel text="Menu 3.A">Menu3A</NextLevel>
    <NextLevel text="Menu 3.B">Menu3B</NextLevel>

Which represents a menu structure like this
Menu 1				Menu 2		Menu 3
	Menu 1.A					Menu 3.A
	Menu 1.B					Menu 3.B
	Menu 1.C

In the link in Solution 1 the Author has used a nice trick to determine which Form to load. I've opted to use Reflection to work out which function to call.

The key is to have all of the routines that could be called from the menus in a simple class.
Here's the one I used
''' <summary>
''' Class DynamicMenuSubs contains all of the menu handlers for dynamically generated menus
''' </summary>
Public Class DynamicMenuSubs
    Public Sub Menu1A()
        Debug.Print("Called Menu1A")
    End Sub
    Public Sub Menu1B()
        Debug.Print("Called Menu1B")
    End Sub
    Public Sub Menu1C()
        Debug.Print("Called Menu1C")
    End Sub
    Public Sub Menu2()
        Debug.Print("Called Menu2")
    End Sub
    Public Sub Menu3A()
        Debug.Print("Called Menu3A")
    End Sub
    Public Sub Menu3B()
        Debug.Print("Called Menu3B")
    End Sub
End Class

Note that the Sub names match (exactly) to the InnerText in the XML above.

Because I only want to visit the database (or XML) once I decided to store the name of the function to call as the Tag of each MenuItem, so here is my code for creating the menus
Private Sub BuildMenus()

    Dim xxml As XmlDocument = GetMenus()

    Dim toplevels As XmlNodeList = xxml.SelectNodes("Menus/TopLevel")
    Dim node As XmlNode
    'Top level menus
    For Each node In toplevels
        Dim tsmi As New ToolStripMenuItem
        tsmi.Text = node.Attributes("text").Value
        Dim sublevels As XmlNodeList = node.SelectNodes("NextLevel")
        If sublevels.Count = 0 Then
            'No sub-menus therefore set the tag of this menuitem to the function name
            tsmi.Tag = node.InnerText
            AddHandler tsmi.Click, AddressOf DynamicMenuHandler
            Dim subnode As XmlNode
            For Each subnode In sublevels
                Dim ddi As New ToolStripMenuItem
                ddi.Text = subnode.Attributes("text").Value
                ddi.Tag = subnode.InnerText
                AddHandler ddi.Click, AddressOf DynamicMenuHandler
        End If
End Sub

A key feature here is that all of the menu items have the same handler for the click event - whether they are top level items or sub-menu items. I called the BuildMenus function from the Form Load event

Here is that handler
Private Sub DynamicMenuHandler(ByVal sender As System.Object, ByVal e As System.EventArgs)
    Dim s1 As ToolStripMenuItem = DirectCast(sender, ToolStripMenuItem)

    'Use Reflection to invoke the named function
    If s1.Tag.ToString.Length > 0 Then
        Dim SubType As System.Type = GetType(DynamicMenuSubs)
        Dim SubInfo As System.Reflection.MethodInfo = SubType.GetMethod(s1.Tag.ToString())
        Dim SubController As New DynamicMenuSubs
        SubInfo.Invoke(SubController, Nothing)
    End If

End Sub

[EDIT]I've finally re-found the source for the idea of having the functions in a separate controller class. Credit for that idea (on which this solution hinges) is due to Joris Bijvoets[^] I can't find his original article but the solution on the link details most of it [/EDIT]
Maciej Los 5-Jan-14 16:44pm
Well done!
CHill60 5-Jan-14 16:50pm
It might become the basis of my first article!! (With a bit more work)
Maciej Los 5-Jan-14 16:54pm
I can't wait ;)
FavouritesToolStripMenuItem1.DropDownItems.Add("New Test")
CHill60 12-Apr-21 8:53am
OP wanted "to create menustrip dynamically". This comes nowhere close to a solution
Hedi X Majid 20-Apr-21 2:05am
MenuStrip1.Items.Add("New MenuStrip")
NewMenuStripToolStripMenuItem.DropDownItems.Add("New DropDownMenuStrip1")

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900