How to add ContextMenuStrip to menu item?

Hi!

How can I add a ContextMenuStrip to a menu item

Matt



Answer this question

How to add ContextMenuStrip to menu item?

  • aaryan

    Do you use the mouse down or mouse up event of the item Can you send me the code

    ~Matt


  • ahmedilyas

    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 :)



  • Tianyu Li

    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 :)



  • SLV

    help!
  • R.Tutus

    so
  • Jerryk

    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.


  • qwv

    But then the contextmenustrip closes.

    Matt


  • Gary Pronych

    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


  • Geer352197

    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 :)



  • Praveen_wm

    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 );
    }
    }

  • None01

    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 :)



  • GrayMatter Software

    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.

  • Rizzen

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


  • How to add ContextMenuStrip to menu item?