none
Menustrip dropdown items RRS feed

  • Question

  • Hi all

    I have a menu strip with severa toolstripmenuitem (TSItem1, TSItem 2 etc)

    TSItem1 has a dropdowm list of items and every one of them has an Item.value ("voice1", "voice2",  etc)

    I need to find index in the dropdown list knowing the item.value (i.e. index of item that has value "voice2"

    How to do it?

    Thursday, December 20, 2018 1:17 PM

Answers

  • Hi,

    Why do you have to declare myIndex first?You can use code:

    CType(TSItem1.DropDownItems.Item("Voice2"), ToolStripMenuItem).CheckState = CheckState.Checked

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Claudio111 Wednesday, December 26, 2018 7:42 AM
    Monday, December 24, 2018 2:53 AM

All replies

  • Please close the following thread and I will give you a solution. You have a habit of not closing threads and opening new ones and this needs to stop.

    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Thursday, December 20, 2018 2:35 PM
    Moderator
  • Just the last one was still open,  others are all closed .

    Thursday, December 20, 2018 2:37 PM
  • Just the last one was still open,  others are all closed .

    But you need to stop this, you do this all the time.

    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Thursday, December 20, 2018 3:02 PM
    Moderator
  • Here is what options you have.

    • I setup a shared click event for the two menu items, more can of course be added. This gives us the index of the selected menu item.
    • In GetMenuItems I show how to iterate the menu item collection. Note I have set the Tag property of each menu item e.g. Voice 1 index 0, Voice 2 index 1 etc.

    Public Class Form1 Private Sub VoiceItem_Click(sender As Object, e As EventArgs) Handles Voice1Item.Click, Voice2Item.Click ' current click item's index Dim Index = CType(sender, ToolStripItem).Owner.Items.IndexOf(CType(sender, ToolStripItem)) Text = $"Selected index: {Index}" End Sub Private Iterator Function GetMenuItems(ByVal item As ToolStripMenuItem) As IEnumerable(Of ToolStripMenuItem) For Each dropDownItem As ToolStripMenuItem In item.DropDownItems ' ' In the IDE designer I set the tag to a numeric value '

    Console.WriteLine($"Name: {dropDownItem.Name}, Text {dropDownItem.Text}, Tag {dropDownItem.Tag} ") If dropDownItem.HasDropDownItems Then For Each subItem As ToolStripMenuItem In GetMenuItems(dropDownItem) Console.WriteLine(subItem.Owner.Name) Yield subItem Next subItem End If Yield dropDownItem Next End Function ''' <summary> ''' Generic iterate menu items (see above), I kept it generic so you can understand ''' how the iterator works. ''' </summary> ''' <param name="sender"></param> ''' <param name="e"></param> Private Sub Button1_Click_1(sender As Object, e As EventArgs) Handles Button1.Click

    ' iterate items Dim allItems As New List(Of ToolStripMenuItem)() For Each toolItem As ToolStripMenuItem In MenuStrip1.Items allItems.Add(toolItem) allItems.AddRange(GetMenuItems(toolItem)) Next End Sub End Class


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Thursday, December 20, 2018 3:06 PM
    Moderator
  • This do no answer to my question.

    I know the Item.name of a dropdown items since I added all of them runtime

    Now I need its index without loop all items in the dropdown list since I have to change its checkedstate runtime. 


    Thursday, December 20, 2018 3:32 PM
  • This do no answer to my question.

    I know the Item.name of a dropdown items since I added all of them runtime

    Now I need its index without loop all items in the dropdown list since I have to change its checkedstate runtime. 


    You need to iterate as shown else you can't get to the index.

    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Thursday, December 20, 2018 4:01 PM
    Moderator
  • Sorry Keren

    I thnk I can avoid to loop thru dropdown  with this statments

    'Find index in dropdown for item with item.name = Voice2
    
    Dim myIndex= TSItem1.DropDownItems.IndexOfKey("Voice2")
    
    'Find Item
    Dim myItem As ToolStripMenuItem = TSItem1.DropDownItems.Item(myindex)
    
    ' change myItem checkstate
    myItem.CheckState = CheckState.Checked


    Am I worng ?




    • Edited by Claudio111 Friday, December 21, 2018 9:25 AM
    Friday, December 21, 2018 9:22 AM
  • Hi,

    Why do you have to declare myIndex first?You can use code:

    CType(TSItem1.DropDownItems.Item("Voice2"), ToolStripMenuItem).CheckState = CheckState.Checked

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Claudio111 Wednesday, December 26, 2018 7:42 AM
    Monday, December 24, 2018 2:53 AM
  • Ok Alex, thanks
    Wednesday, December 26, 2018 7:42 AM