[VB.NET] DirectCast ToolStripMenuItem


Description
      i will explain about directcast to toolstripmenuitem, is it possible ?? until now and search google i don't find any method how to direct cast using "string" into toolstripmenuitem. but i found another method to control  toolstripmenuitem.

How To
the first thing you must know how depth your menu !! , ok let's see the explanation below
for example you have menustrip like this :

file                                        Edit            Help
   - user namagement :
           - change password
----------------------------
   - exit

the max depth above 3 , with 1 main menu and 2 sub menu :
main menu = File,Edit,Help
Submenu - depth 1 = User Management,Exit
Submenu - Depth 2 = Change Password

based on information above, we get max depth = 3 so just create 3 variable with type  toolstripmenuitem
Dim mainmenu As ToolStripMenuItem
Dim submenudepth1 As ToolStripMenuItem
Dim submenudepth2 As ToolStripMenuItem

after that you can parsing every item collection using foreach and "In" and don't forget add method ofType to find the item using specific type. why we use that because sparator different type with ToolStripMenuItem, here's the code

For Each mainmenu In frmMain.MenuStrip.Items.OfType(Of ToolStripMenuItem)()
    ' code
next

if you already understand the code above , you can learn easily the code below, here's the full code
Dim mainmenu As ToolStripMenuItem
Dim submenudepth1 As ToolStripMenuItem
Dim submenudepth2 As ToolStripMenuItem
 For Each mainmenu In frmMain.MenuStrip.Items.OfType(Of ToolStripMenuItem)()
        For Each submenudepth1 In mainmenu.DropDownItems.OfType(Of ToolStripMenuItem)()
               For Each submenudepth2 In submenudepth1.DropDownItems.OfType(Of ToolStripMenuItem)()
                       If submenudepth2.Name = "mnuchangepassword" Then
                                ' if specific menu is found using the name (condition)
                                'do you code
                                for example
                                submenudepth2.enabled = true
                       Else
                                ' just leave , the default i set is disabled
                       End If
                 Next
         Next
Next


here's the full example :
            Dim objDatabase As New database
            Dim connectionString, strsql As String
            Dim dataUser As DataTable
            Dim i As Integer

            Dim mainmenu As ToolStripMenuItem
            Dim submenudepth1 As ToolStripMenuItem
            Dim submenudepth2 As ToolStripMenuItem

            connectionString = "FILE NAME=" & My.Application.Info.DirectoryPath & "\SQLServer.udl"
            strsql = "select * from MS_USER_GROUP_DETAIL where GROUP_NAME = '" & CurrentRole & "'"
            dataUser = objDatabase.runningQuery(connectionString, strsql)

            ' ------------------------------------------
            ' MENU ASSIGN - Please edit your menu below

            If dataUser.Rows.Count > 0 Then
                i = 0  'index start from 0
                For Each mainmenu In frmMain.MenuStrip.Items.OfType(Of ToolStripMenuItem)()
                    For Each submenudepth1 In mainmenu.DropDownItems.OfType(Of ToolStripMenuItem)()
                        For Each submenudepth2 In submenudepth1.DropDownItems.OfType(Of ToolStripMenuItem)()
                            For Each fields As DataRow In dataUser.Rows
                                If i >= dataUser.Rows.Count Then
                                    'maka out of index
                                Else
                                    For Each itemName In dataUser.Rows
                                        'item(2) display record at column index no 2 (List of menu name)
                                        If itemName(2).ToString = submenudepth2.Name Then
                                            'if same name then enabled
                                            submenudepth2.Enabled = True
                                            i = i + 1
                                        Else
                                            ' just leave , the default is disabled
                                        End If
                                    Next
                                End If
                            Next
                        Next
                    Next
                Next
                i = 0 'reset index
            Else
            End If
        Catch err As Exception
            MsgBox(err.Message, vbCritical, "Application Crash")
        End Try

the code above very handy if you make user management with specific group and each group have many items that linking into menu.

Post a Comment

Harap gunakan bahasa yang baik dan sopan, terima kasih