Newbie: How to find out which menu item has been clicked from a context menu?
-
This is a follow on question from: http://www.codeproject.com/script/comments/forums.asp?forumid=1649&XtraIDs=1649&searchkw=hodges&sd=5%2F16%2F2006&ed=8%2F14%2F2006&select=1619517&df=100&noise=5&mpp=50&fr=319#xx1619517xx I have been able to populate a context menu with data from a database. However I now have the problem of working out which item has been selected from the menu. This is the code that adds the items to the menu:
private void addPersonalPhonebookItem(string name, string number) { ToolStripMenuItem personal = ts_phonebook_personal; ToolStripMenuItem personalMenuItem = new ToolStripMenuItem(); personalMenuItem.Name = name; personalMenuItem.Text = name + " (" + number + ")"; personalMenuItem.Click +=new EventHandler(phonebookMenuItem_Click); personalMenuItem.Tag = name + number; personal.DropDown.Items.Add(personalMenuItem); }
And this is the EventHandler:void phonebookMenuItem_Click(object sender, EventArgs e) { MessageBox.Show("You Just Clicked: " + e.ToString()); }
How do I get the 'name' or 'number' of the clicked item... Thanks again in advance, Phil -
This is a follow on question from: http://www.codeproject.com/script/comments/forums.asp?forumid=1649&XtraIDs=1649&searchkw=hodges&sd=5%2F16%2F2006&ed=8%2F14%2F2006&select=1619517&df=100&noise=5&mpp=50&fr=319#xx1619517xx I have been able to populate a context menu with data from a database. However I now have the problem of working out which item has been selected from the menu. This is the code that adds the items to the menu:
private void addPersonalPhonebookItem(string name, string number) { ToolStripMenuItem personal = ts_phonebook_personal; ToolStripMenuItem personalMenuItem = new ToolStripMenuItem(); personalMenuItem.Name = name; personalMenuItem.Text = name + " (" + number + ")"; personalMenuItem.Click +=new EventHandler(phonebookMenuItem_Click); personalMenuItem.Tag = name + number; personal.DropDown.Items.Add(personalMenuItem); }
And this is the EventHandler:void phonebookMenuItem_Click(object sender, EventArgs e) { MessageBox.Show("You Just Clicked: " + e.ToString()); }
How do I get the 'name' or 'number' of the clicked item... Thanks again in advance, Philvoid phonebookMenuItem_Click(object sender, EventArgs e)
{
ToolStripMenuItem mnu = (sender as ToolStripMenuItem);
MessageBox.Show("You just clicked: " + mnu.Name);
}:rolleyes: (Hmmm, ever wandered what the
sender
property did?)
Formula 1 - Short for "F1 Racing" - named after the standard "help" key in Windows, it's a sport where participants desperately search through software help files trying to find actual documentation. It's tedious and somewhat cruel, most matches ending in a draw as no participant is able to find anything helpful. - Shog9 Ed
-
void phonebookMenuItem_Click(object sender, EventArgs e)
{
ToolStripMenuItem mnu = (sender as ToolStripMenuItem);
MessageBox.Show("You just clicked: " + mnu.Name);
}:rolleyes: (Hmmm, ever wandered what the
sender
property did?)
Formula 1 - Short for "F1 Racing" - named after the standard "help" key in Windows, it's a sport where participants desperately search through software help files trying to find actual documentation. It's tedious and somewhat cruel, most matches ending in a draw as no participant is able to find anything helpful. - Shog9 Ed
Thanks for the quick reply... Great stuff... Phil:-D
-
Thanks for the quick reply... Great stuff... Phil:-D
Not a problem, at least you explained your problem more thouroughly than other people. (And also said thanks which is a nice change :))
Formula 1 - Short for "F1 Racing" - named after the standard "help" key in Windows, it's a sport where participants desperately search through software help files trying to find actual documentation. It's tedious and somewhat cruel, most matches ending in a draw as no participant is able to find anything helpful. - Shog9 Ed