I haven't programmed in VB for a while so I'm a bit rusty.
I have an XML that I'm reading that contains a list of projects. When I load my form, I populate a toolstrip menu (as well as a combo box which works fine) with the project names. I also dynamically populate a submenu for each project with "Edit" and "Delete". I need to get the index of the project that was selected so I can call certain code for the edit and delete. I'm not quite sure how to do this.
Here is the code:
Public Sub loadXML()
' Load the XML file.
xml_doc.Load(Application.StartupPath & "\Settings.xml")
' Get the desired children.
child_nodes = xml_doc.GetElementsByTagName("project")
' Fill the project combo box and to the project menu
cmbProjects.Items.Clear()
For Each child As System.Xml.XmlElement In child_nodes
cmbProjects.Items.Add(child.SelectSingleNode("projectname").InnerText)
Dim project As New ToolStripMenuItem(child.SelectSingleNode("projectname").InnerText)
Dim edit As New ToolStripMenuItem("Edit")
Dim delete As New ToolStripMenuItem("Delete")
project.DropDownItems.Add(edit)
project.DropDownItems.Add(delete)
ProjectsMenu.DropDownItems.Add(project)
AddHandler edit.Click, AddressOf editProject
AddHandler delete.Click, AddressOf deleteProject
Next
Private Sub editProject(ByVal sender As System.Object, ByVal e As System.EventArgs)
MsgBox("Editing project...")
' Need to find the index of the project here so I can call the edit for that specific project
End Sub
Private Sub deleteProject(ByVal sender As System.Object, ByVal e As System.EventArgs)
MsgBox("Deleting project...")
' Need to find the index of the project here so I can call the delete for that specific project
End Sub
To answer your question as asked:
Dim child = DirectCast(sender, ToolStripMenuItem)
Dim parent = DirectCast(child.OwnerItem, ToolStripMenuItem)
Dim grandparent = DirectCast(parent.OwnerItem, ToolStripMenuItem)
Dim parentIndex = grandparent.DropDownItems.IndexOf(parent)