8/27/2014 Dynamic Creation Of MenuStrip - VB.
NET - CodeProject
10,821,399 members Sign in
home articles quick answers discussions features community help Search for articles, questions, tips
Articles » Languages » VB.NET » General
Dynamic Creation Of MenuStrip - VB.NET
Bad Programmer, 23 Aug 2008 Rate this:
3.76 (13 votes)
Implementing Menustrip dynamically from database, the menu names and order will be through backend.
Download menustrip - 1.53 KB
Download tables - 3.75 KB
Introduction
I came up with the problem of creating an application that was driven according to user rights. I thought of
implementing the menus to be driven through BackEnd. All the menu names and the form (formname) to open when
the child menu was clicked, come from the table. To implement menus, I used MenuStrip from VB.NET.
Sample Code
Using the Code
It executes during the Form Load and populates all the menu headers. All data comes from the backend and there is
nothing to hardcode. I have attached the structure of two tables MENUMASTER and ACCESS below.
The code below will have a variable like iUserAccessMode, which in turn tells us about the user access level (Look
into the Excel sheet which has been attached and look into the Access Tab in the Excel sheet). The Menus will be
loaded according to User Access Level.
Collapse | Copy Code
Private Sub MDIParent1_Load(ByVal sender As Object,
ByVal e As System.EventArgs) Handles Me.Load
'On Error GoTo ErrHandler
'Create a Connection class, that has the full features of working with Backend.
'See article for Connection class : Connection_Class.asp
Dim Conn As New Conn1
Dim mnRd As SqlClient.SqlDataReader
'It helps in populating the MENUs according to user rights. From the Table "Access"
iUserAccessMode = GlobalValues.lblUsrAccess.Text
sQry = ""
sQry = "Select MenuText from MenuMaster Where MainMenuID = 0" & _
" And MenuID in (Select MenuID from Access Where AccessId =
" & CInt(iUserAccessMode) & ")" & _
" And isActive = 1"
mnRd = Conn.ReaderData(sQry)
If mnRd.HasRows Then
mnMenu = New MenuStrip
While mnRd.Read
mnMenu.Items.Add(mnRd(0).ToString, Nothing, New System.EventHandler(
AddressOf MainMenu_OnClick))
Me.Controls.Add(mnMenu)
End While
End If
mnRd.Close()
End Sub
http://www.codeproject.com/Articles/19223/Dynamic-Creation-Of-MenuStrip-VB-NET 1/4
8/27/2014 Dynamic Creation Of MenuStrip - VB.NET - CodeProject
This function creates child menus when the parent menu is created.
Collapse | Copy Code
Private Sub MainMenu_OnClick(ByVal sender As Object, ByVal e As System.EventArgs)
Dim cms As New ContextMenuStrip()
Dim sMenu() As String
Dim Conn As New Conn1
Dim sMenuRD As SqlClient.SqlDataReader
sQry = ""
sQry = "Select MenuID from MenuMaster Where MenuText = '" & sender.ToString & "'"
sMenuRD = Conn.ReaderData(sQry)
Dim parentMenuID As Integer
If sMenuRD.HasRows Then
sMenuRD.Read()
parentMenuID = sMenuRD("MenuID")
End If
sMenuRD.Close()
sQry = ""
sQry = "Select MenuText from MenuMaster Where MainMenuID =
'" & parentMenuID & "'" & _
" And isActive = 1" & _
" And MenuID in (
Select MenuID from Access Where AccessId =
" & CInt(iUserAccessMode) & ")" & _
" Order BY MenuOrder"
sMenuRD = Conn.ReaderData(sQry)
ReDim Preserve sMenu(0)
Dim i As Integer
If sMenuRD.HasRows Then
ReDim Preserve sMenu(0) Article
i = 0
While sMenuRD.Read() Browse Code
ReDim Preserve sMenu(i)
Stats
sMenu(i) = sMenuRD("MenuText")
i = i + 1
Revisions
End While
End If Alternatives
sMenuRD.Close()
For Each sMn As String In sMenu Comments (48)
cms.Items.Add(sMn, Nothing,
New System.EventHandler(AddressOf SelectedChildMenu_OnClick))
Next
Dim tsi As ToolStripMenuItem = CType(sender,
Tagged as ToolStripMenuItem)
tsi.DropDown = cms
End Sub
.NET2.0
VS2005
This function is executed at the click event of each menu item, the form to open(FormName) on the execution of the
event also comes from the backend table "MENUMASTER VB8.0 ".
Windows
Collapse | Copy Code
.NETCF
Private Sub SelectedChildMenu_OnClick(ByVal sender As Object,
ByVal e As System.EventArgs) Dev
Dim Conn As New Conn1 Intermediate
Dim sMenuRD As SqlClient.SqlDataReader
Dim frmName As String = ""
Dim frm As New Form
sQry = ""
sQry = "Select FormName from MenuMaster Where MenuText = '" &
sender.ToString & "'"
sMenuRD = Conn.ReaderData(sQry)
If sMenuRD.HasRows Then
sMenuRD.Read()
frmName = sMenuRD(0).ToString
DynamicallyLoadedObject(frmName).Show(Me)
Else
MsgBox("Under Construction", MsgBoxStyle.Exclamation, "Technical Error")
End If
sMenuRD.Close()
End Sub
Points of Interest
I came up with the problem of converting the formname which I get from SQL as string to FORM object. This
function helps in converting the string object formName, which is returned from SQL into object of type Form:
Collapse | Copy Code
Private Function DynamicallyLoadedObject(ByVal objectName As String,
Optional ByVal args() As Object = Nothing) As Form
Dim returnObj As Object = Nothing
Dim Type As Type = Assembly.GetExecutingAssembly().GetType(
"[YOUR PROJECT NAME]." & objectName)
If Type IsNot Nothing Then
returnObj = Activator.CreateInstance(Type, args)
End If
Return returnObj
End Function
History
17th June, 2007: Initial post
License
This article, along with any associated source code and files, is
licensed under The Code Project Open License (CPOL)
Share
EMAIL
You may also be interested in...
http://www.codeproject.com/Articles/19223/Dynamic-Creation-Of-MenuStrip-VB-NET 2/4
8/27/2014 Dynamic Creation Of MenuStrip - VB.NET - CodeProject
5 Proven Ways to Motivate and Retain Your Technology Team
About the Author
No Biography provided
Bad Programmer
United Kingdom
Article Top
Comments and Discussions
You must Sign In to use this message board.
Search Comments Go
Profile popups Spacing Compact Noise Medium Layout Normal Per page 50 Update
First Prev Next
sample solution Member 10503568 4-Jan-14 10:50
Object reference not set to an instance of an object ajay1706 15-Dec-12 21:50
Please help... shahidzahoor 22-Aug-12 20:32
Probleme sur menu dynamique abraoui 20-Jul-12 2:42
iUserAccessMode ? shahidzahoor 18-Jul-12 9:30
how to add icon to the menustrip item siva7784 18-Aug-11 0:39
Object reference not set to an instance of an object. Alicealiciasawarak 28-Mar-11 15:23
Re: Object reference not set to an instance of an object. TheXeon1981 12-Nov-11 5:26
Name 'Assembly' is not declared Alicealiciasawarak 28-Mar-11 2:12
Re: Name 'Assembly' is not declared Bad Programmer 31-Mar-11 12:03
how to show the 3 level menu y2yawar 12-Jan-10 19:10
MainMenu twice click from first form load Member 2728683 24-Jul-09 1:28
Re: MainMenu twice click from first form load Juan Camilo Arboleda 1-Aug-09 16:10
Re: MainMenu twice click from first form load Mark Denson 16-Nov-09 6:58
Re: MainMenu twice click from first form load wallaces528 16-Nov-09 16:52
Dynamic Creation Of MenuStrip - C#.NET SameeraMadhawa 23-Jul-09 22:34
Re: Dynamic Creation Of MenuStrip - C#.NET Moises Torres 10-Sep-09 10:44
Problem in DynamicallyLoadedObject() Arul Soosai 12-Apr-09 21:07
Re: Problem in DynamicallyLoadedObject() Arul Soosai 12-Apr-09 21:32
Re: Problem in DynamicallyLoadedObject() Bad Programmer 13-Apr-09 1:03
Re: Problem in SelectedchildMenu_OnClick event Arul Soosai 14-Apr-09 22:00
Re: Problem in SelectedchildMenu_OnClick event Bad Programmer 18-Apr-09 18:27
Re: Problem in DynamicallyLoadedObject() Alicealiciasawarak 28-Mar-11 18:38
Re: Problem in DynamicallyLoadedObject() Alicealiciasawarak 30-Mar-11 20:55
Dynamic Creation of MenuStrip- C#.net [modified] Shahid K 5-Mar-09 1:24
Result listed by vertical, not horizontal Hendra Prasetyo 15-Jan-09 16:59
Re: Result listed by vertical, not horizontal Bad Programmer 15-Jan-09 20:51
open dynamic form in MDI window Mithuya 4-Nov-08 8:05
Re: open dynamic form in MDI window Bad Programmer 5-Nov-08 1:13
Re: open dynamic form in MDI window Mark Denson 16-Nov-09 7:01
Re: open dynamic form in MDI window Finder115 11-Jun-11 21:10
Re: open dynamic form in MDI window Finder115 11-Jun-11 20:51
Excelent lefaconi 25-Oct-08 5:06
Re: Excelent Mithuya 4-Nov-08 8:04
Simple superb ashu fouzdar 26-Aug-08 20:43
Download code bkresimir 25-Feb-08 19:30
Hacking Access database Gyanendra kumar 29-Jun-07 19:07
http://www.codeproject.com/Articles/19223/Dynamic-Creation-Of-MenuStrip-VB-NET 3/4
8/27/2014 Dynamic Creation Of MenuStrip - VB.NET - CodeProject
thakur
Re: Hacking Access database Bala J 30-Jun-07 9:43
Use the 'new' keyword to create an object instance. keonj 26-Jun-07 12:00
Re: Use the 'new' keyword to create an object instance. Bala J 30-Jun-07 11:17
Re: Use the 'new' keyword to create an object instance. Dustin Klinkenberg 20-Aug-08 7:53
Re: Use the 'new' keyword to create an object instance. Alicealiciasawarak 28-Mar-11 19:00
Create a Connection class GeertD 19-Jun-07 0:52
Re: Create a Connection class Bad Programmer 5-Nov-08 1:22
Re: Create a Connection class GeertD 5-Nov-08 1:49
Re: Create a Connection class Bad Programmer 5-Nov-08 19:47
Article missing pic Jared James Sullivan 17-Jun-07 16:29
Re: Article missing pic Bala_se 18-Jun-07 10:20
Last Visit: 31-Dec-99 18:00 Last Update: 26-Aug-14 13:33 Refresh 1
General News Suggestion Question Bug Answer Joke Rant Admin
Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.
Permalink | Advertise | Privacy | Mobile Layout: fixed | fluid Article Copyright 2007 by Bad Programmer
Web04 | 2.8.140826.1 | Last Updated 24 Aug 2008 Everything else Copyright © CodeProject, 1999-2014
Terms of Service
http://www.codeproject.com/Articles/19223/Dynamic-Creation-Of-MenuStrip-VB-NET 4/4