Is it possible to get the index of an item in a dropdown menu?
-
Please elaborate, there is not enough information.– Petar MinchevCommented Sep 27, 2010 at 14:29
-
I'm not sure if I understand what you're trying to do. Could you provide more details about your specific scenario?– Claudio RediCommented Sep 27, 2010 at 14:30
-
What kind of dropdown menu? WPF/Forms, etc.?– jsmithCommented Sep 27, 2010 at 14:30
-
I have a menu item called "Projects" and everytime i create a new project or open a project it is added to this menuitem as a Dropdown item. When i then click the dropdown item i want to pass along the index of the menu item.– BildsoeCommented Sep 28, 2010 at 7:33
Add a comment
|
2 Answers
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);
}
}
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);
}
}