Reputation: 1340
Is it possible to get the index of an item in a dropdown menu?
Upvotes: 2
Views: 8618
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
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