Get ToolStripMenuItem index
I have this code to add ToolStripMenuItems when my form loads.
Code:
Dim i As Integer = 0
Dim stanicastring As String
While i < ListBox1.Items.Count
stanicastring = ListBox1.Items.Item(i)
listastanica.DropDownItems.Add(stanicastring)
i = i + 1
End While
It adds all items from my ListBox to my ToolStripMenuItem and it works fine. But now i need the same item to be selected in my ListBox when I click any item in ToolStripMenuItem (all items in ListBox and ToolStripMenuItem are the same). The problem is that ListBox items are user changeable. I wanted to find ToolStripMenuItem index and make ListBox selected index the same, but i can't seem to find ToolStripMenuItem index property. Can anyone please help me? And one more thing. How do I make something happen when i click ToolStripMenuItem? Once again, ToolStripMenuItems are dynamic, which means users can change them. It would be easy to do that if items were static... Please answer me. Thank you.
Re: Get ToolStripMenuItem index
try this:
vb Code:
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim i As Integer = 0
Dim stanicastring As String
While i < ListBox1.Items.Count
stanicastring = ListBox1.Items(i).ToString
listastanica.DropDownItems.Add(stanicastring)
AddHandler listastanica.DropDownItems(i).Click, AddressOf item_Click
i = i + 1
End While
End Sub
Private Sub item_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
ListBox1.SelectedIndex = listastanica.DropDownItems.IndexOf(DirectCast(sender, ToolStripItem))
'this is the ToolStripItem_Click event handler
'put any code you want to execute here
End Sub
End Class
Re: Get ToolStripMenuItem index
Guys, I already figured it out, but thanks anyway...