Results 1 to 3 of 3

Thread: Get ToolStripMenuItem index

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2010
    Posts
    8

    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.

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,422

    Re: Get ToolStripMenuItem index

    try this:

    vb Code:
    1. Public Class Form1
    2.  
    3.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    4.         Dim i As Integer = 0
    5.         Dim stanicastring As String
    6.  
    7.         While i < ListBox1.Items.Count
    8.             stanicastring = ListBox1.Items(i).ToString
    9.             listastanica.DropDownItems.Add(stanicastring)
    10.             AddHandler listastanica.DropDownItems(i).Click, AddressOf item_Click
    11.             i = i + 1
    12.         End While
    13.     End Sub
    14.  
    15.     Private Sub item_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
    16.         ListBox1.SelectedIndex = listastanica.DropDownItems.IndexOf(DirectCast(sender, ToolStripItem))
    17.         'this is the ToolStripItem_Click event handler
    18.         'put any code you want to execute here
    19.     End Sub
    20.  
    21. End Class

  3. #3

    Thread Starter
    New Member
    Join Date
    Oct 2010
    Posts
    8

    Re: Get ToolStripMenuItem index

    Guys, I already figured it out, but thanks anyway...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width