|
-
October 26th, 2004, 10:40 AM
#1
Context Menu (submenu) question
Hi,
I am creating a submenu at the runtime and this submenu can have many items (the number is not known at compile time). I want to how to create event handler for these menuitems. Is there a generalized way to handle the events on these submenu items? Please look at the following code and suggest:
Code:
public void GetAllHierarchyList(MenuItem mnuFile)
{
mnuFile.MenuItems.Clear();
foreach (HierarchyNode h in this.hierarchy.HierarchieNodes)
{
mnuFile.MenuItems.Add (h.Type); //want event handler for all these
//items
}
}
-
October 26th, 2004, 03:59 PM
#2
Re: Context Menu (submenu) question
After adding the menu item
Code:
mnuFile.MenuItems.Add (h.Type);
add an event handler
Code:
mnuFile.MenuItems.Click += new System.EventHandler(MenuItem_Click);
Add the event handler
Code:
private void MenuItem_Click(object sender, EventArgs e)
{
// Add code here
}
-
October 26th, 2004, 04:10 PM
#3
Re: Context Menu (submenu) question
I tried your code but it gives error:
error CS0117: 'System.Windows.Forms.Menu.MenuItemCollection' does not contain a definition for 'Click'
for the line
Code:
mnuFile.MenuItems.Click += new System.EventHandler(this.MenuItemAddExisiting_Click);
-
October 27th, 2004, 02:21 AM
#4
Re: Context Menu (submenu) question
RaviGubbi had the right idea. As you've found out MenuItemCollection does not understand Click but MenuItem does. So
Code:
mnuFile.MenuItems.Add (h.Type, new System.EventHandler(this.MenuItemAddExisting_Click));
OR
MenuItem menuItem = new MenuItem(h.Type);
menuItem.Click += new System.EventHandler(this.MenuItemAddExisting_Click);
mnuFile.MenuItems.Add(menuItem);
The next problem you have is deciding what to do when a menuItem is clicked...
-
October 27th, 2004, 10:01 AM
#5
Re: Context Menu (submenu) question
Thanks. Help me out a little more.
How to find out info about the menuitem that's clicked from the handler/
Code:
private void MenuItem_Click(object sender, EventArgs e)
{
// Add code here
}
Thanks
-
October 27th, 2004, 10:08 AM
#6
Re: Context Menu (submenu) question
Code:
MenuItem menuItem = sender as MenuItem;
-
October 27th, 2004, 10:09 AM
#7
Re: Context Menu (submenu) question
-
October 27th, 2004, 10:26 AM
#8
Re: Context Menu (submenu) question
Since I got a thankyou, consider this:
Code:
foreach (HierarchyNode node in this.hierarchy.HierarchieNodes)
{
// create a handler for each node
MenuItemHandler handler = new MenuItemHandler(node);
// ...and assign it to a menu item
MenuItem menuItem = new MenuItem(node.type);
menuItem.Click += new System.EventHandler(handler.MenuItem_Click);
mnuFile.MenuItems.Add (menuItem);
}
private class MenuItemHandler
{
public MenuItemHandler(HierarchyNode node)
{
// TODO store whatever you need
}
public void MenuItem_Click(object sender, EventArgs e)
{
// Since one-to-one relation ship with menuItem no need to ask who the
// sender is.
DoSomething();
}
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|