Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. How to create menuItems dynamic on Popup event?

How to create menuItems dynamic on Popup event?

Scheduled Pinned Locked Moved C#
csharptutorialquestion
5 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    Chris Richner
    wrote on last edited by
    #1

    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]:.

    W 1 Reply Last reply
    0
    • C Chris Richner

      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]:.

      W Offline
      W Offline
      Wizard_01
      wrote on last edited by
      #2

      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.

      C 1 Reply Last reply
      0
      • W Wizard_01

        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.

        C Offline
        C Offline
        Chris Richner
        wrote on last edited by
        #3

        Hi thanks, i will try it, but it only works for me if the sub menuitems also fire this event. .:[Greetz from Jerry Maguire]:.

        H 1 Reply Last reply
        0
        • C Chris Richner

          Hi thanks, i will try it, but it only works for me if the sub menuitems also fire this event. .:[Greetz from Jerry Maguire]:.

          H Offline
          H Offline
          Heath Stewart
          wrote on last edited by
          #4

          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 own ContextMenu 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-----

          B 1 Reply Last reply
          0
          • H Heath Stewart

            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 own ContextMenu 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-----

            B Offline
            B Offline
            Bryan White
            wrote on last edited by
            #5

            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

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups