Create menu at runtime .Net Or Create dynamic menu vb.net Or Create runtime menu vb
From here you could download project. In this access database is used to keep menu structure and menu i build run time where project is executed.
Download project from here, Click Here.
Imports System.Data.OleDb
Public Class Form2
Dim cmd As OleDbCommand
Dim dr As OleDbDataReader
Dim con As OleDbConnection
Dim dsitems As New DataSet
Dim da As OleDbDataAdapter
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim str As String
str = "Provider=Microsoft.ACE.OLEDB.12.0;data source=" & Application.StartupPath & "\database1.mdb"
con = New OleDbConnection(str)
con.Open()
loadmenuitems()
End Sub
Private Sub loadmenuitems()
dsitems = GetDataSet()
For Each drtemp As DataRow In dsitems.Tables(0).Select("MenuParent='' and USERID='DEMO'")
CreateMenuItem(drtemp(0).ToString())
Next
End Sub
Private Sub CreateMenuItem(ByVal strMenu As String)
Dim mmru As MenuItem = New MenuItem(GetMenuName(strMenu), New EventHandler(AddressOf Menu_Click))
mmru.Enabled = GetMenuEnable(strMenu)
mnuMainMenu.MenuItems.Add(mmru)
If dsitems.Tables(0).Select("MenuParent='" + strMenu + "' and USERID='DEMO'").Length > 0 Then
For Each drTemp As DataRow In dsitems.Tables(0).Select("MenuParent='" + strMenu + "' and USERID='DEMO'")
CreateMenuItems(mmru, drTemp(0).ToString())
Next
End If
End Sub
Private Function GetMenuName(ByVal strMenuID As String) As String
Return dsitems.Tables(0).Select("MenuID='" + strMenuID + "'")(0)(1).ToString()
End Function
Private Function GetMenuEnable(ByVal strMenuID As String) As Boolean
If dsitems.Tables(0).Select("MenuID='" + strMenuID + "'")(0)(3).ToString() = "Y" Then
Return True
Else
Return False
End If
End Function
Private Function CreateMenuItems(ByVal mnuItem As MenuItem, ByVal strMenu As String) As String
If dsitems.Tables(0).Select("MenuParent='" + strMenu + "'").Length > 0 Then
Dim mmru As MenuItem = New MenuItem(GetMenuName(strMenu), New EventHandler(AddressOf Menu_Click))
mmru.Enabled = GetMenuEnable(strMenu)
mnuItem.MenuItems.Add(mmru)
For Each drTemp1 As DataRow In dsitems.Tables(0).Select("MenuParent='" & strMenu & "' and USERID='DEMO'")
CreateMenuItems(mmru, drTemp1(0).ToString())
Next
Else
Dim mmru As MenuItem = New MenuItem(GetMenuName(strMenu), New EventHandler(AddressOf Menu_Click))
mmru.Enabled = GetMenuEnable(strMenu)
mnuItem.MenuItems.Add(mmru)
Return strMenu
End If
Return strMenu
End Function
Private Sub Menu_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Select Case DirectCast(sender, MenuItem).Text.ToUpper()
Case "TXT"
MessageBox.Show("You clicked - Text File")
Exit Select
Case "OPEN"
MessageBox.Show("You clicked - Open")
Exit Select
Case "LOGIN"
MessageBox.Show("You clicked - LOGIN")
Exit Select
Case "EXIT"
Me.Close()
Exit Select
End Select
End Sub
Private Function GetDataSet() As DataSet
Dim ds As New DataSet("Menu")
Dim drTmp As DataTable = ds.Tables.Add("Menu")
drTmp.Columns.Add("MenuID", GetType(String))
drTmp.Columns.Add("MenuName", GetType(String))
drTmp.Columns.Add("MenuParent", GetType(String))
drTmp.Columns.Add("MenuEnable", GetType(String))
drTmp.Columns.Add("USERID", GetType(String))
Dim str As String
Dim strarr(5) As String
str = "Select * from menutable"
cmd = New OleDbCommand(str, con)
dr = cmd.ExecuteReader
While dr.Read
strarr(0) = dr(0).ToString
strarr(1) = dr(1).ToString
strarr(2) = dr(2).ToString
strarr(3) = dr(3).ToString
strarr(4) = dr(4).ToString
drTmp.Rows.Add(New Object() {strarr(0), strarr(1), strarr(2), strarr(3), strarr(4)})
End While
Return ds
End Function
End Class
Download database only, Click Here.
Download project from here, Click Here.