Mr Bim
Mr Bim

Reputation: 45

vb.net: Menu Strip not returning the Name/Index of Sub-Menu

I have a Menu Strip called "Contact" containing Customer, Supplier, and Contact. I create 3 sub-menu using code based on the permission user, the menu looks like that:

enter image description here

And if I click 1 of them (let's say if I click "Customer" inside the Contact menu it will return the Name or the Index) it will show the form.

I already try to iterate the item by using this:

MsgBox(DirectCast(sender, ToolStripMenuItem).Text)

or

MsgBox(DirectCast(sender, ToolStripDropDownItem).Text)

but it always returns "Contact" but I expect to get a sub item if I click one of them. Is there any way to do what I expected? Thank you :)

Update

Here is the code I use to add some submenu:

Private Sub LoadMenu(ByVal strMenuSet As String)
    'This function is used to show the menu based on the clicked menu with the permission already set'

    Dim cText, cPerm As Object
    Dim cMenu, cMenu0, cMenu1, cMenu2, cMenu3, cMenu4, cMenu5, cMenu6, cMenu7, cMenu8, cMenu9 As Object
    Dim intLoad As Integer
    Dim intI As Integer

    cMenu = mnu
    cMenu0 = mnu0
    cMenu1 = mnu1
    cMenu2 = mnu2
    cMenu3 = mnu3
    cMenu4 = mnu4
    cMenu5 = mnu5
    cMenu6 = mnu6
    cMenu7 = mnu7
    cMenu8 = mnu8
    cMenu9 = mnu9 
'if there is a button is clicked, it will trigger the menu and show in the menu strip'
Select Case strMenuSet
            Case "Editor"
                cMenu.ToString()
                With cMenu
                    cText = {"&File", "Contact", "Items", "Machines", "Tools", "", "", "", "", "", "", ""}
                    cPerm = {"Perm1", "Perm2", "Perm3", "Perm4", "Perm5"}
                    For intI = 0 To 9
                        If cText(intI) = "" Then
                            .Items(intI).Enabled = False
                            .Items(intI).Visible = False
                        Else
                            If fPermission(par1, par2, par3) Then
                                .Items(intI).Text = cText(intI)
                                .Items(intI).Enabled = True
                                .Items(intI).Visible = True
                            Else
                                .Items(intI).Enabled = False
                                .Items(intI).Visible = False
                            End If
                        End If
                    Next
                End With
'and if I click "Contact", it will generate submenu like the picture I attach before'

       With cMenu1
         cText = {"*", "Customer", "Supplier", "Contact"}
         cPerm = {"Perm1", "Perm2", "Perm3", "Perm4"}
         For intI = 0 To 1
           If intI = 0 Then
              intLoad = 0
              .DropDownItems.Add(".")
              .DropDownItems(intLoad).Visible = False
           Else
              If fPermission(lngPubUserId, cPerm(intI), "prmOpen") Then
                intLoad += 1
                .DropDownItems.Add(intLoad).Text = cText(intI)
                .DropDownItems(intLoad).Enabled = True
                .DropDownItems(intLoad).Visible = True
              Else
                .DropDownItems(intLoad).Enabled = False
                .DropDownItems(intLoad).Visible = False
              End If
            End If
           Next
          End With
EndSub

This is the event handler:

Private Sub mnu1_Click(sender As Object, e As EventArgs) Handles mnu1.Click
    
    MsgBox(DirectCast(val.OwnerItem, ToolStripMenuItem).Text)

End Sub

This is the relevant code I use.

Upvotes: 0

Views: 98

Answers (0)

Related Questions