vb.net winform动态生成菜单(源码)

效果图:



源码如下:

Private Sub CreateMenu(ByVal dt As DataTable)

        Dim mainMenu As New MenuStrip
        Dim dv As DataView = dt.DefaultView
        dv.RowFilter = "MenuLevel='1'"

        For i As Integer = 0 To dv.Count - 1
            Dim topMenu As New ToolStripMenuItem
            '给菜单赋Text值。也就是在界面上看到的值。
            topMenu.Text = dv(i).Item("MenuText").ToString
            '如果是有下级菜单则通过CreateSubMenu方法来创建下级菜单
            If dv(i).Item("SubMenu") = "1" Then
                '以ref的方式将顶层菜单传递参数
                CreateSubMenu(topMenu, dv(i).Item("MenuID"), dt)
            End If
            '显示应用程序中已打开的 MDI 子窗体列表的菜单项  
            mainMenu.MdiWindowListItem = topMenu
            '将递归附加好的菜单加到菜单根项上
            mainMenu.Items.Add(topMenu)
        Next
        mainMenu.Dock = DockStyle.Top
        '这句很重要。如果不写这句菜单将不会出现在主窗体中。
        Me.Controls.Add(mainMenu)
    End Sub

    Private Sub CreateSubMenu(ByRef topMenu As ToolStripMenuItem, ByVal itemID As Integer, ByVal dt As DataTable)
        Dim dv As DataView = dt.AsDataView
        '过滤出当前父菜单下在所有子菜单数据(仅为下一层的)
        dv.RowFilter = "MenuPre='" & itemID.ToString + "'"

        For i As Integer = 0 To dv.Count - 1
            '创建子菜单项
            Dim subMenu As New ToolStripMenuItem
            subMenu.Text = dv(i).Item("MenuText")
            '如果还有子菜单则继续递归加载
            If dv(i).Item("SubMenu") = "1" Then
                '以ref的方式将顶层菜单传递参数
                CreateSubMenu(subMenu, dv(i).Item("MenuID"), dt)
            Else
                '扩展属性可以加任何想要的值。这里用MenuForm属性来加载窗体。
                subMenu.Tag = dv(i).Item("MenuForm")
                '给没有子菜单的菜单项加事件。
                AddHandler subMenu.Click, AddressOf subMenu_click
            End If

            topMenu.DropDownItems.Add(subMenu)

        Next
    End Sub

    Private Sub subMenu_click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Dim formName As String = CType(sender, ToolStripMenuItem).Tag.ToString
        CreateFormInstance(formName)
    End Sub

    Private Sub CreateFormInstance(ByVal strFormName As String)
        Dim flag As Boolean = False
        '遍历主窗口上的所有子菜单  
        For i As Integer = 0 To Me.MdiChildren.Length - 1
            '如果所点的窗口被打开则重新激活
            If Me.MdiChildren(i).Tag.ToString().ToLower = strFormName.ToLower Then
                Me.MdiChildren(i).Activate()
                Me.MdiChildren(i).Show()
                Me.MdiChildren(i).WindowState = FormWindowState.Normal
                flag = True
                Exit For
            End If
        Next
        If flag = False Then
            '如果不存在则用反射创建form窗体实例
            Dim asm As Assembly = Assembly.Load("winMain")
            Dim objForm As Object = asm.CreateInstance("winMain." + strFormName)
            Dim frm As New Form
            frm = CType(objForm, Form)
            frm.TopLevel = False
            frm.Parent = Me.panelForm
            frm.Show()

        End If
    End Sub

    Private Function dtMenu() As DataTable
        Dim dt As New DataTable

        '菜单ID
        Dim column0 As New DataColumn
        column0.DataType = System.Type.GetType("System.String")
        column0.AllowDBNull = False
        column0.Caption = "菜单ID"
        column0.ColumnName = "MenuID"

        '菜单级别
        Dim column1 As New DataColumn
        column1.DataType = System.Type.GetType("System.String")
        column1.AllowDBNull = False
        column1.Caption = "菜单级别"
        column1.ColumnName = "MenuLevel"

        '菜单名称
        Dim column2 As New DataColumn
        column2.DataType = System.Type.GetType("System.String")
        column2.AllowDBNull = False
        column2.Caption = "菜单名称"
        column2.ColumnName = "MenuName"

        '菜单显示文本
        Dim column3 As New DataColumn
        column3.DataType = System.Type.GetType("System.String")
        column3.AllowDBNull = False
        column3.Caption = "菜单显示文本"
        column3.ColumnName = "MenuText"

        '上级菜单
        Dim column4 As New DataColumn
        column4.DataType = System.Type.GetType("System.String")
        column4.AllowDBNull = False
        column4.Caption = "上级菜单"
        column4.ColumnName = "MenuPre"

        '子菜单
        Dim column5 As New DataColumn
        column5.DataType = System.Type.GetType("System.String")
        column5.AllowDBNull = False
        column5.Caption = "子菜单"
        column5.ColumnName = "SubMenu"

        '菜单打开窗体
        Dim column6 As New DataColumn
        column6.DataType = System.Type.GetType("System.String")
        column6.AllowDBNull = False
        column6.Caption = "菜单打开窗体"
        column6.ColumnName = "MenuForm"

        dt.Columns.Add(column0)
        dt.Columns.Add(column1)
        dt.Columns.Add(column2)
        dt.Columns.Add(column3)
        dt.Columns.Add(column4)
        dt.Columns.Add(column5)
        dt.Columns.Add(column6)

        Dim row As DataRow
        row = dt.NewRow()
        row("MenuID") = "1"
        row("MenuLevel") = "1"
        row("MenuName") = "menu1"
        row("MenuText") = "一级菜单1"
        row("MenuPre") = "0"
        row("SubMenu") = "1"
        row("MenuForm") = "1"
        dt.Rows.Add(row)

        row = dt.NewRow()
        row("MenuID") = "2"
        row("MenuLevel") = "1"
        row("MenuName") = "menu2"
        row("MenuText") = "一级菜单2"
        row("MenuPre") = "0"
        row("SubMenu") = "1"
        row("MenuForm") = "1"
        dt.Rows.Add(row)

        row = dt.NewRow()
        row("MenuID") = "3"
        row("MenuLevel") = "1"
        row("MenuName") = "menu3"
        row("MenuText") = "一级菜单3"
        row("MenuPre") = "0"
        row("SubMenu") = "1"
        row("MenuForm") = "1"
        dt.Rows.Add(row)

        row = dt.NewRow()
        row("MenuID") = "4"
        row("MenuLevel") = "2"
        row("MenuName") = "menu4"
        row("MenuText") = "一级菜单1二级菜单1"
        row("MenuPre") = "1"
        row("SubMenu") = "0"
        row("MenuForm") = "frmTest"
        dt.Rows.Add(row)

        row = dt.NewRow()
        row("MenuID") = "5"
        row("MenuLevel") = "2"
        row("MenuName") = "menu5"
        row("MenuText") = "一级菜单1二级菜单2"
        row("MenuPre") = "1"
        row("SubMenu") = "1"
        row("MenuForm") = "1"
        dt.Rows.Add(row)

        row = dt.NewRow()
        row("MenuID") = "6"
        row("MenuLevel") = "2"
        row("MenuName") = "menu6"
        row("MenuText") = "一级菜单2二级菜单1"
        row("MenuPre") = "2"
        row("SubMenu") = "0"
        row("MenuForm") = "1"
        dt.Rows.Add(row)

        row = dt.NewRow()
        row("MenuID") = "7"
        row("MenuLevel") = "3"
        row("MenuName") = "menu7"
        row("MenuText") = "一级菜单1二级菜单2三级菜单1"
        row("MenuPre") = "5"
        row("SubMenu") = "0"
        row("MenuForm") = "1"
        dt.Rows.Add(row)

        row = dt.NewRow()
        row("MenuID") = "8"
        row("MenuLevel") = "3"
        row("MenuName") = "menu8"
        row("MenuText") = "一级菜单1二级菜单2三级菜单2"
        row("MenuPre") = "5"
        row("SubMenu") = "0"
        row("MenuForm") = "1"
        dt.Rows.Add(row)

        Return dt

    End Function


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值