locked
How to add ContextMenuStrip to menu item? RRS feed

  • Question

  • Hi!

    How can I add a ContextMenuStrip to a menu item?

    Matt

    Wednesday, August 30, 2006 2:28 AM

Answers

  • Don't do it, your users won't discover this by themselves in a million years.  If you insist: implement the MouseDown event for the menu item and call the Show() method...
    Wednesday, August 30, 2006 2:51 AM

All replies

  • Don't do it, your users won't discover this by themselves in a million years.  If you insist: implement the MouseDown event for the menu item and call the Show() method...
    Wednesday, August 30, 2006 2:51 AM
  • nobugz is right, it is a giant waste of time.  Your average joe will never right click on a menu.  Ever.  The reason for this is that a context menu is just a menu that you can access from any location you want instead of a fixed location on a toolstrip.  And a menustrip is just a context menu in drop down form from a toolstrip or whatever.  Your context menu should be a sub menu off of the menu item you want it to show up on. 
    Wednesday, August 30, 2006 3:04 AM
  • I can see the value in advanced customization options, i..e. customizing the menu itself.

    You can see this behaviour in the Windows Start Menu. Try right-clicking on entries.  
    Wednesday, August 30, 2006 9:46 AM
  • When I add a contextmenu to a menu item using the mouseup event and then add contextMenuStrip.Show();, it closes the opened menu strip and opens the contextmenu strip. How can I make the dropped down menu strip visible and still show the context menu strip.

    Matt

    Saturday, September 30, 2006 3:27 PM
  • help!
    Sunday, October 1, 2006 10:06 PM
  • Do this:

    menuItem1ToolStripMenuItem.DropDown.Show();

    That should make it drop down the list again, mabee put it int he click event you amde that shows the context menustrip or somthin...

     

     

    Hope this helps :)

    Sunday, October 1, 2006 10:29 PM
  • But then the contextmenustrip closes.

    Matt

    Monday, October 2, 2006 3:06 AM
  • hmm, I made a new project and tested it..it works for me...

    Where did you put it, after or before the contextmenustrip show?

    If you put it before that, then put it after it.

     

     

    Hope this helps :)

    Monday, October 2, 2006 11:17 PM
  • Do you use the mouse down or mouse up event of the item? Can you send me the code?

    ~Matt

    Tuesday, October 3, 2006 2:02 AM
  • I used the click event...

    oh, I see what you mean now by it closes, I re-entered the code and it only keep one menu or the other open...Becuase the context menustrip is a menustrip, and the program always closes the currently opened menustrip to go to the next, right, so I think thats why...I will see if I can get them both to stay open, and I will tell you what I get later on today.

    But why do you need the other one open at the same time?

     

     

     

    Hope this helps :)

    Tuesday, October 3, 2006 2:54 PM
  • so?
    Tuesday, October 3, 2006 8:23 PM
  • I dont think you can do it this way, unless you make your own user control or custom control of a menustrip and make it be able to have more then one menu open... ... ...

     

     

    Hope this helps :)

    Tuesday, October 3, 2006 11:59 PM
  • You can use this derivated menuItem...

            public class ContextSpecialToolStripMenuItem : ToolStripMenuItem
            {
                private bool _showContextMenu = false;
                private bool _hasDropDownItems = false;
                private ToolStripDropDown _realDropDown = null;

                private ContextMenuStrip _contextMenuStrip = null;
                public ContextMenuStrip ContextMenuStrip
                {
                    get { return _contextMenuStrip; }
                    set { _contextMenuStrip = value; }
                }

                private Point _dropDownLocation = Point.Empty;
                protected override Point DropDownLocation
                {
                    get
                    {
                        if (_showContextMenu)
                            return _dropDownLocation;
                        else
                            return base.DropDownLocation;
                    }
                }

                public Point PointToScreen( Point p )
                {
                    Point result = Point.Empty;
                    Point location = this.Bounds.Location;
                    ToolStrip parent = this.Parent;
                    if (parent == null)
                        parent = (this.IsOnOverflow && (this.Owner != null)) ? this.Owner.OverflowButton.DropDown : this.Owner;

                    if (parent != null)
                    {
                        p.X += location.X;
                        p.Y += location.Y;
                        return parent.PointToScreen( p );
                    }

                    return p;
                }

                public override bool HasDropDownItems
                {
                    get
                    {
                        if (_showContextMenu)
                            return _hasDropDownItems;
                        else
                            return base.HasDropDownItems;
                    }
                }

                protected override void OnDropDownClosed( EventArgs e )
                {
                    base.OnDropDownClosed( e );
                    if (_showContextMenu)
                    {
                        this.DropDown = _realDropDown;
                        _showContextMenu = false;
                        this.HideDropDown();
                    }
                }

                protected override void OnMouseDown( MouseEventArgs e )
                {
                    if (_contextMenuStrip != null && e.Button == MouseButtons.Right && e.Clicks == 1)
                    {
                        _realDropDown = this.DropDown;
                        _hasDropDownItems = base.HasDropDownItems;
                        _showContextMenu = true;

                        this.DropDown = _contextMenuStrip;
                        _dropDownLocation = this.PointToScreen( e.Location );
                        this.ShowDropDown();
                    }
                    base.OnMouseDown( e );
                }
            }
    Monday, July 9, 2007 3:18 PM