注册 登录
编程论坛 VB.NET论坛

希望通过递归遍历菜单,然后根据权限修改菜单的enabled属性

梦幻倩影 发布于 2016-09-17 19:12, 2788 次点击
希望通过递归遍历菜单,然后根据权限修改菜单的enabled属性,弄了一下午都不行,谁能帮忙写一下遍历菜单的递归
9 回复
#2
不说也罢2016-09-17 20:56
好吧,来个简单的例子,希望能够帮到你。

程序代码:

   Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        For Each MyMenu In Me.MenuStrip1.Items
            GetChildMenu(MyMenu)
        Next
    End Sub

    Private Sub GetChildMenu(ByVal TheMenu As ToolStripMenuItem)
        If TheMenu.HasDropDownItems Then
            For Each m As ToolStripMenuItem In TheMenu.DropDownItems
                GetChildMenu(m)
            Next
        Else
            MsgBox(TheMenu.Text)   '楼主分配权限的代码可以写在这里
        End If
    End Sub
#3
梦幻倩影2016-09-18 09:05
出错,提示:
无法将类型为“System.Windows.Forms.ToolStripSeparator”的对象强制转换为类型“System.Windows.Forms.ToolStripMenuItem

因为菜单中有.ToolStripSeparator 如何判断是.ToolStripSeparator还是ToolStripMenuItem?
就因为这个我搞不定
#4
不说也罢2016-09-18 09:38
修改下GetChildMenu函数就可以了呀。楼主问题没有表述清楚哦。

程序代码:


    Private Sub GetChildMenu(ByVal TheMenu As ToolStripMenuItem)
        If TheMenu.HasDropDownItems Then
            For Each m In TheMenu.DropDownItems '注意这里的修改
                If m.GetType.Name <> "ToolStripSeparator" Then GetChildMenu(m) '注意这里的判断
            Next
        Else
            MsgBox(TheMenu.Text)     '楼主分配权限的代码可以写在这里
        End If
    End Sub

#5
梦幻倩影2016-09-18 11:36
终于搞定,谢谢版主
#6
梦幻倩影2016-09-18 11:42
灰色的菜单是无权使用的
只有本站会员才能查看附件,请 登录
#7
梦幻倩影2016-09-26 17:21
Public Function getmenu(ByVal dc As ToolStripMenuItem) As Integer
        Dim sqlstr As String = ""
        Dim objConnection As New SqlConnection(sqlstr)
        Dim sql As String = ""
        ' 打开连接SqlConnection
        Try
            If dc.HasDropDownItems Then
                For Each m In dc.DropDownItems
                    If m.GetType.Name <> "ToolStripSeparator" Then
                        getmenu(m)
                    End If
                Next
            Else
                qxi = qxi + 1
            End If
            '开始保存菜单到SQL
            sqlstr = getsqlstr()
            ' 创建数据库
            objConnection = New SqlConnection(sqlstr)
            If objConnection.State <> ConnectionState.Open Then
                objConnection.Open()
            End If
            sql = "Insert into mymenu(id,菜单名,变量名) values(" + Str(qxi) + ",'" + Trim(dc.Text) + "','" + Trim(dc.Name) + "')" + vbCrLf
            Dim cmd As SqlCommand = New SqlCommand(sql, objConnection)
            Try
                cmd.ExecuteNonQuery()
            Catch ae As SqlException
                MessageBox.Show(ae.Message.ToString())
            End Try
            objConnection.Close()
            objConnection.Dispose()
            objConnection = Nothing
        Catch ex As Exception
        End Try
        Return 0
    End Function

请问上面的递归如何去掉有子菜单的菜单名称呢?就是不要将refger2,fafar,srrfree 添加进记录里?
#8
梦幻倩影2016-09-26 17:31
这是上面函数导出来的菜单,后面ID号重复的,就是因为将有带子菜单的菜单名称也加进来了,如何去掉这些

菜单名    变量名    id
retgreyh1    RetgreyhToolStripMenuItem    1
reyrtytru2    ReyrtytruToolStripMenuItem    2
trujytij3    TrujytijToolStripMenuItem    3
tuytiu4    TuytiuToolStripMenuItem    4
yiuyiuyoi5    TyiiToolStripMenuItem    5
tyiuyiyuou6    TyiuyiyuouToolStripMenuItem    6
ytiuyot7    YtiuyotToolStripMenuItem    7
rtyhrtyu    RtyhrtyuToolStripMenuItem    7
rtgr8    RtgrToolStripMenuItem    8
retg9    RetgToolStripMenuItem    9
uyiouoi    UyiouoiuToolStripMenuItem    9
yuikuyo10    YuikuyoToolStripMenuItem    10
yiouop11    YiouopToolStripMenuItem    11
rtytru    RtytruToolStripMenuItem    11
uyioiu12    UyioiuToolStripMenuItem    12
rtuityi    RtuityiToolStripMenuItem    12
tyujtyiu13    TyujtyiuToolStripMenuItem    13
fggfjhng14    ToolStripMenuItem6    14
fgjhgjf15    ToolStripMenuItem7    15
gfjhg16    GfjhgToolStripMenuItem    16
gfjhg17    GfjhgToolStripMenuItem1    17
dfgrd18    Dfgrd18ToolStripMenuItem    18
dfgvbfdhb19    Dfgvbfdhb19ToolStripMenuItem    19
dfghfrtrthj    ToolStripMenuItem1    19
uyoiuo20    UyoiuoToolStripMenuItem    20
ytiuyi21    YtiuyiToolStripMenuItem    21
ytiyu22    YtiyuToolStripMenuItem    22
tyiuyi23    TyiuyiToolStripMenuItem    23
yuoiuo    YuoiuoToolStripMenuItem    23
uyioup24    UyioupToolStripMenuItem    24
yuiou25    YuiouToolStripMenuItem    25
ytujytiu26    YtujytiuToolStripMenuItem    26
tyuytiuy27    TyuytiuyToolStripMenuItem    27
tyuiy28    TyuiyToolStripMenuItem    28
yuioo    YuiooToolStripMenuItem    28
uyikuyo29    ToolStripMenuItem4    29
uydfthj30    UydfthjToolStripMenuItem    30
dvdxfgvbf31    Dvdxfgvbf33ToolStripMenuItem    31
uyiouiu    ToolStripMenuItem2    31
新建(&N)    NewToolStripMenuItem    32
打开(&O)    OpenToolStripMenuItem    33
保存(&S)    SaveToolStripMenuItem    34
另存为(&A)    SaveAsToolStripMenuItem    35
打印(&P)    PrintToolStripMenuItem    36
打印预览(&V)    PrintPreviewToolStripMenuItem    37
打印设置    PrintSetupToolStripMenuItem    38
退出(&X)    ExitToolStripMenuItem    39
文件(&F)    FileMenu    39
撤消(&U)    UndoToolStripMenuItem    40
重复(&R)    RedoToolStripMenuItem    41
剪切(&T)    CutToolStripMenuItem    42
复制(&C)    CopyToolStripMenuItem    43
粘贴(&P)    PasteToolStripMenuItem    44
全选(&A)    SelectAllToolStripMenuItem    45
编辑(&E)    EditMenu    45
工具栏(&T)    ToolBarToolStripMenuItem    46
状态栏(&S)    StatusBarToolStripMenuItem    47
视图(&V)    ViewMenu    47
选项(&O)    OptionsToolStripMenuItem    48
工具(&T)    ToolsMenu    48
新建窗口(&N)    NewWindowToolStripMenuItem    49
层叠(&C)    CascadeToolStripMenuItem    50
垂直平铺(&V)    TileVerticalToolStripMenuItem    51
水平平铺(&H)    TileHorizontalToolStripMenuItem    52
全部关闭(&L)    CloseAllToolStripMenuItem    53
排列图标(&A)    ArrangeIconsToolStripMenuItem    54
设置背景图    设置背景图ToolStripMenuItem    55
读取菜单名称    读取菜单名称ToolStripMenuItem    56
窗口(&W)    WindowsMenu    56
目录(&C)    ContentsToolStripMenuItem    57
索引(&I)    IndexToolStripMenuItem    58
搜索(&S)    SearchToolStripMenuItem    59
关于(&A) ...    AboutToolStripMenuItem    60
帮助(&H)    HelpMenu    60
#9
不说也罢2016-09-26 17:31
写入数据库代码放在If dc.HasDropDownItems Then这个条件不成立时即可。
#10
梦幻倩影2016-09-26 21:33
谢谢,终于去掉了,还是菜单的属性不熟悉啊
1