Tom Knowlton
asked on
Context Menus 101
I have added a Context Menu (popup) to a Windows Form.
At runtime....when I right-click on the associated control....the popup menu activates and shows "Add"
But when I click on Add...nothing happens because I have not associated an event handler for the click on Add.
How do I do this?
At runtime....when I right-click on the associated control....the popup menu activates and shows "Add"
But when I click on Add...nothing happens because I have not associated an event handler for the click on Add.
How do I do this?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Ohhhhhh...........I get it.....duh...............
This works:
private void contextMenuTreeMod_Popup(o bject sender, System.EventArgs e)
{
// Define the MenuItem objects to display for the TextBox.
MenuItem menuItem1 = new MenuItem("&Add");
menuItem1.Click += new EventHandler(menuItem1_Cli ck); ////////////////////////// ////////// JUST NEEDED TO ADD THIS PART......AS YOU SUGGESTED.....//////////// ////////// //
// Clear all previously added MenuItems.
contextMenuTreeMod.MenuIte ms.Clear() ;
if(contextMenuTreeMod.Sour ceControl == treeViewInventory)
{
// Add MenuItems to display for the TextBox.
contextMenuTreeMod.MenuIte ms.Add(men uItem1);
}
}
protected void menuItem1_Click(System.Obj ect sender, System.EventArgs e)
{
// Insert code to handle Click event.
MessageBox.Show("Add something to");
}
This works:
private void contextMenuTreeMod_Popup(o
{
// Define the MenuItem objects to display for the TextBox.
MenuItem menuItem1 = new MenuItem("&Add");
menuItem1.Click += new EventHandler(menuItem1_Cli
// Clear all previously added MenuItems.
contextMenuTreeMod.MenuIte
if(contextMenuTreeMod.Sour
{
// Add MenuItems to display for the TextBox.
contextMenuTreeMod.MenuIte
}
}
protected void menuItem1_Click(System.Obj
{
// Insert code to handle Click event.
MessageBox.Show("Add something to");
}
ASKER
Let me ask you this:
How would I UNWIRE the ADD click event from one method............. and then REWIRE the ADD click event to some other method:
My best guess:
private void contextMenuTreeMod_Popup(o bject sender, System.EventArgs e)
{
// Define the MenuItem objects to display for the TextBox.
MenuItem menuItem1 = new MenuItem("&Add");
menuItem1.Click += new EventHandler(menuItem1_Cli ck); ////////////////////////// ////////// JUST NEEDED TO ADD THIS PART......AS YOU SUGGESTED.....//////////// ////////// //
//CAN I USE -= to unassign the event handler?????????????????
// Clear all previously added MenuItems.
contextMenuTreeMod.MenuIte ms.Clear() ;
if(contextMenuTreeMod.Sour ceControl == treeViewInventory)
{
// Add MenuItems to display for the TextBox.
contextMenuTreeMod.MenuIte ms.Add(men uItem1);
}
}
protected void menuItem1_Click(System.Obj ect sender, System.EventArgs e)
{
// Insert code to handle Click event.
MessageBox.Show("Add something to");
}
protected void menuItem1Alt_Click(System. Object sender, System.EventArgs e)
{
// Insert code to handle Click event.
MessageBox.Show("ALTERNATE Add something to");
}
How would I UNWIRE the ADD click event from one method............. and then REWIRE the ADD click event to some other method:
My best guess:
private void contextMenuTreeMod_Popup(o
{
// Define the MenuItem objects to display for the TextBox.
MenuItem menuItem1 = new MenuItem("&Add");
menuItem1.Click += new EventHandler(menuItem1_Cli
//CAN I USE -= to unassign the event handler?????????????????
// Clear all previously added MenuItems.
contextMenuTreeMod.MenuIte
if(contextMenuTreeMod.Sour
{
// Add MenuItems to display for the TextBox.
contextMenuTreeMod.MenuIte
}
}
protected void menuItem1_Click(System.Obj
{
// Insert code to handle Click event.
MessageBox.Show("Add something to");
}
protected void menuItem1Alt_Click(System.
{
// Insert code to handle Click event.
MessageBox.Show("ALTERNATE
}
Yes, you can use -= to remove delegates. You can also wire up multiple event handlers if you need to.
ASKER
wire-up multiple event handlers?
you mean several differnt event handlers for one click....and the one that gets used depends on something?
can you explain?
you mean several differnt event handlers for one click....and the one that gets used depends on something?
can you explain?
Multicasting is the ability to call multiple methods with a single delegate.
EventHandler a = new EventHandler(method1);
EventHandler b = new EventHandler(method2);
EventHandler c = new EventHandler(method3);
menuItem.Click = a + b + c;
Then you can add or remove them with += or -=.
EventHandler a = new EventHandler(method1);
EventHandler b = new EventHandler(method2);
EventHandler c = new EventHandler(method3);
menuItem.Click = a + b + c;
Then you can add or remove them with += or -=.
ASKER
Interesting.
ASKER
private void contextMenuTreeMod_Popup(o
{
// Define the MenuItem objects to display for the TextBox.
MenuItem menuItem1 = new MenuItem("&Add");
// Clear all previously added MenuItems.
contextMenuTreeMod.MenuIte
if(contextMenuTreeMod.Sour
{
// Add MenuItems to display for the TextBox.
contextMenuTreeMod.MenuIte
}
}
My intent is for THIS method to fire when the user clicks on ADD..........but the method is not WIRED to the CLICK EVENT
protected void menuItem1_Click(System.Obj
{
// Insert code to handle Click event.
MessageBox.Show("Add something to");
}