How to create menuItems dynamic on Popup event?
-
Hi, I would like to fill subitems on the fly of a ContextMenu. But it seems like the dot net menuItem class does not support this. The Popup event is never fired. I fill my contextmenu with menuitems, which only has one dummy sub menuitem. Now i need an event to remove the dummy and populate the real items. Any hints? .:[Greetz from Jerry Maguire]:.
-
Hi, I would like to fill subitems on the fly of a ContextMenu. But it seems like the dot net menuItem class does not support this. The Popup event is never fired. I fill my contextmenu with menuitems, which only has one dummy sub menuitem. Now i need an event to remove the dummy and populate the real items. Any hints? .:[Greetz from Jerry Maguire]:.
-
Try this :
this.ContextMenu.Popup += new EventHandler(ContextMenu_Popup); ... private void ContextMenu_Popup(object sender, EventArgs e) { this.ContextMenu.MenuItems.Add(new MenuItem("ContextMenuPopup")); }
I tried and it worked.Hi thanks, i will try it, but it only works for me if the sub menuitems also fire this event. .:[Greetz from Jerry Maguire]:.
-
Hi thanks, i will try it, but it only works for me if the sub menuitems also fire this event. .:[Greetz from Jerry Maguire]:.
If not (I don't think it does, either), then learn a lesson from COM and have your objects that need to customize the context menu implement an interface of your design. Override the context menu nature (like preventing it from popping up automatically), check for the interface of the object that's being activated, and - for example - pass the
ContextMenu
to an interface method in order for the implementation to customize it. Then show the popup. There's many ways you can accomplish this, but basically take the approach that the active object will modify it's ownContextMenu
based on its state or the state of objects to which it's related.-----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----
-
If not (I don't think it does, either), then learn a lesson from COM and have your objects that need to customize the context menu implement an interface of your design. Override the context menu nature (like preventing it from popping up automatically), check for the interface of the object that's being activated, and - for example - pass the
ContextMenu
to an interface method in order for the implementation to customize it. Then show the popup. There's many ways you can accomplish this, but basically take the approach that the active object will modify it's ownContextMenu
based on its state or the state of objects to which it's related.-----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----
This is how I do it: - create a ContextMenuPopup in the application (I do this in the base form, but use it for multiple forms in the same application). I think that I used the GUI designer originally: this.contextMenuPopup = new System.Windows.Forms.ContextMenu(); - the GUI designer threw this code in; how much is actually used? dunno! this.contextMenuPopup.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] { this.menuItemTitle, this.menuItemSep1, this.menuItemOpt1, this.menuItemOpt2, this.menuItemOpt3}); this.contextMenuPopup.Popup += new System.EventHandler(this.contextMenuPopup_Popup); - In the form you want it to apply to, add: newWindowL.GraphicControl.ContextMenu = contextMenuPopup; - Write a menu builder This is tricky; you need to remember that: - each MenuItem has to be a clone, not an original, if you are "mix'n'match"ing a menu. Well, I found that it helped 'cos some items are duplicated. - each MenuItem needs an event handler. I use a recursive method to allow multi-level context menus -MenuItemCommand is derived from MenuItem & does a few fancy things like storing a command object (cf "Command Patterns") The code has just been copied & pasted. Use it more for ideas/principles than 'as is'; here goes: #region ContextMenuPopup stuff private void contextMenuPopup_Popup(object sender, System.EventArgs e) { ContextMenu ctxMenu = sender as ContextMenu; if(ctxMenu == null) return; Control ctl = ctxMenu.SourceControl; if(ctl == null) return; // blah blah contextMenuBuilder.SetContextOptionNames(GarageSystem.Garage, hitStruct4ContextMenu, mEditingMenus, contextMenuPopup); } public void SetContextOptionNames(SEComponentMaster topLevel, Structure hitStruct, Menu.MenuItemCollection theMenuCollection, ContextMenu theContextMenu)//, MenuClicker theEventHandler) { MenuClicker theEventHandler = new