locked
How to get the item index of a Menustrip item RRS feed

  • Question

  • Hi good day to all; 

    Just want to ask on how to retrieve the index of selected item in the menustrip control, 

    For example i have menustrip name "Object"

    and have 3 items name "BALL","PIN", and "TOY"

    I have variable int "Index" 

    When I click "BALL" the value of "Index" variable must be their index


    • Moved by CoolDadTx Tuesday, July 18, 2017 5:19 PM Winforms related
    Monday, July 17, 2017 6:25 AM

Answers

  • Hi Tin,

    As far as I know, the menuitems are used as a single control, I'm afraid there is not a single event to achieve your purpose, that means you must write some code in each menuitem's click event, and you can use the code Viorel provided:

            int Index;
    
            private void TSMItemBALL_Click(object sender, EventArgs e)
            {
                Index = ((ToolStripItem)sender).Owner.Items.IndexOf((ToolStripItem)sender);
                textBox1.Text = Index.ToString();
            }
    
            private void TSMItemPIN_Click(object sender, EventArgs e)
            {
                Index = ((ToolStripItem)sender).Owner.Items.IndexOf((ToolStripItem)sender);
                textBox1.Text = Index.ToString();
            }
    
            private void TSMItemTOY_Click(object sender, EventArgs e)
            {
                Index = ((ToolStripItem)sender).Owner.Items.IndexOf((ToolStripItem)sender);
                textBox1.Text = Index.ToString();
            }

    Hope this helps!

    Best Regards,

    Stanly


    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.

    • Proposed as answer by Stanly Fan Wednesday, July 19, 2017 5:56 AM
    • Marked as answer by TinVin727 Tuesday, July 25, 2017 12:15 AM
    Wednesday, July 19, 2017 5:55 AM

All replies

  • Handle the Click event of menu item and try this code:

       Index = ( (ToolStripItem)sender ).Owner.Items.IndexOf( (ToolStripItem)sender );

    • Proposed as answer by Stanly Fan Wednesday, July 19, 2017 5:56 AM
    Monday, July 17, 2017 6:59 AM
  • Hi Tin,

    As far as I know, the menuitems are used as a single control, I'm afraid there is not a single event to achieve your purpose, that means you must write some code in each menuitem's click event, and you can use the code Viorel provided:

            int Index;
    
            private void TSMItemBALL_Click(object sender, EventArgs e)
            {
                Index = ((ToolStripItem)sender).Owner.Items.IndexOf((ToolStripItem)sender);
                textBox1.Text = Index.ToString();
            }
    
            private void TSMItemPIN_Click(object sender, EventArgs e)
            {
                Index = ((ToolStripItem)sender).Owner.Items.IndexOf((ToolStripItem)sender);
                textBox1.Text = Index.ToString();
            }
    
            private void TSMItemTOY_Click(object sender, EventArgs e)
            {
                Index = ((ToolStripItem)sender).Owner.Items.IndexOf((ToolStripItem)sender);
                textBox1.Text = Index.ToString();
            }

    Hope this helps!

    Best Regards,

    Stanly


    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.

    • Proposed as answer by Stanly Fan Wednesday, July 19, 2017 5:56 AM
    • Marked as answer by TinVin727 Tuesday, July 25, 2017 12:15 AM
    Wednesday, July 19, 2017 5:55 AM
  • Hi Tin,

    So have you solved this problem now?

    I think the above replies can provide you with a solution, have you tried them?

    If so, hope you can close this thread by marking the reply as answer as this will help others looking for the same or similar issues down the road.

    Best Regards,

    Stanly


    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.

    Friday, July 21, 2017 6:08 AM
  • Thank you Stanly you right i need to do it one by one, 
    Tuesday, July 25, 2017 12:16 AM