Bildsoe
Bildsoe

Reputation: 1340

C#: Is it possible to get the index of a DropDownItem in a menu item?

Is it possible to get the index of an item in a dropdown menu?

Upvotes: 2

Views: 8618

Answers (2)

Luis
Luis

Reputation: 3033

The above solution may throw a null when casting to ToolStripMenuItem and there's some fluff. This one is cleaner:

private void item_Click(object sender, EventArgs e)
{
    ToolStripMenuItem item = sender as ToolStripMenuItem;
    if (item != null)
    {
        int index = ContextMenuStrip.Items.IndexOf(commentMenuItem);
    }
}

Upvotes: 2

devnull
devnull

Reputation: 2860

private void item_Click(object sender, EventArgs e)
{
    ToolStripMenuItem item = sender as ToolStripMenuItem;
    if (item != null)
    {
        int index = (item.OwnerItem as ToolStripMenuItem).DropDownItems.IndexOf(item);
    }
}

Upvotes: 7

Related Questions