I am trying to catch a click event on a context menu submenu created dynamically by the following code. The context menu cmList is created in the designer, and the click event code is added from the properties menu.
for (int i = 0; i <= sTagsContext.GetUpperBound(0); i++)
{
cmListTags.Items.Add(sTagsContext[i]);
ToolStripMenuItem submenu = new ToolStripMenuItem();
submenu.Text = i.ToString();
submenu.Image = Properties.Resources.InfoBig;
(cmListTags.Items[i] as ToolStripMenuItem).DropDownItems.Add(submenu);
chkListTags.ContextMenuStrip = cmListTags;
}
How can I create code to be executed when the submenu of any of the context menu items is clicked and have the identity of the submenu item (set in the text property) available?
I have tried adding an event handler using
(cmListTags.Items[i] as ToolStripMenuItem).DropDownItems.Add(i.ToString(), Properties.Resources.InfoBig, new EventHandler(InfoClicked));
where I create the function
public void InfoClicked(object sender, EventArgs e)
{
}
This function is called when the sub-menu is clicked but neither sender nor e has any information about the sub-menu item clicked - sender is null and e is empty.
If I set e to be type ToolStripItemClickedEventArgs and change the Dropdown addition line to
(cmListTags.Items[i] as ToolStripMenuItem).DropDownItems.Add(i.ToString(), Properties.Resources.InfoBig, new ToolStripItemClickedEventHandler(InfoClicked));
I get a compile time type mismatch for the last parameter of DropDownItems.Add.