MenuItem multiple parent
-
12-12-2019 - |
Question
I have a menu item that appears when right clicking over a datagridview. From their the user hovers over a menuitem and a list of other menu items appears, once again this repeats. Giving soemthing like this.
---------
| FOO |---------
---------| BAR |------------
---------| FOOBAR |
------------
There is only to be an event (addressof) handled on the the 3rd tier. With that being said I need to grab the parent.name from FOOBAR and the parent.name of the said parent (grandparent).
Here is where I am at:
If currentMouseRow >= 0 AndAlso currentMouseColumn <= 1 Then
dataGridView_monitorMapping.Rows(currentMouseRow).Selected = True
mainMenu.MenuItems.Add(New MenuItem("Set Monitor(s) Settings"))
mainMenu.MenuItems.Add(New MenuItem("Sync Monitor Mapping View", AddressOf triggerSync))
'list avaliable priorites
For Each priorityRow As DataRow In priorityTypesDS.Tables(0).Rows
Dim rowPriortiyName As String = CStr(priorityRow("Priority"))
Dim subMenu_priorities_item As New MenuItem(rowPriortiyName)
mainMenu.MenuItems(0).MenuItems.Add(subMenu_priorities_item)
'list avaliable boards
For Each boardRow As DataRow In serviceBoardDS.Tables(0).Rows
Dim rowBoardName As String = CStr(boardRow("SvcBrd"))
Dim subMenu_boards_item As New MenuItem(rowBoardName)
subMenu_priorities_item.MenuItems.Add(subMenu_boards_item)
'list avaliable types based on board
If rowBoardName IsNot Nothing Then
Dim availableSvcTypes As DataSet = GetServiceTypes(_objhost, serviceTypes, rowBoardName)
For Each svcTypeRow As DataRow In availableSvcTypes.Tables(0).Rows
Dim rowSvcTypeName As String = CStr(svcTypeRow(1))
Dim subMenu_svcType_item As New MenuItem(rowSvcTypeName, AddressOf triggerSync)
subMenu_boards_item.MenuItems.Add(subMenu_svcType_item)
Next
End If
Next
Next
mainMenu.Show(dataGridView_monitorMapping, New Point(e.X, e.Y))
End If
And the event handler
Public Sub updateMultiRowSettingChange(ByVal Sender As System.Object, ByVal e As System.EventArgs)
'TODO | Handle selection from right click menu.
End Sub
Solution
Something like this should get you what you are looking for:
Public Sub updateMultiRowSettingChange(ByVal Sender As System.Object, ByVal e As System.EventArgs)
Dim item As MenuItem = CType(sender, MenuItem)
Dim parent As MenuItem = CType(item.Parent, MenuItem)
Dim grandparent As Menu = parent.Parent
End Sub
However, it may be simpler to set the tag on the menu item to whatever information you need to know where it's coming from, then you could just check item.Tag
after you cast it to the MenuItem class.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow