How to add ContextMenuStrip to menu item?

Hi!

How can I add a ContextMenuStrip to a menu item?

Matt

[84 byte] By [MateuszRajca] at [2007-12-25]
# 1
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...
nobugz at 2007-10-8 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 2
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.
DaveS.Anderson at 2007-10-8 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 3
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.

JimBlackler at 2007-10-8 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 4

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

MateuszRajca at 2007-10-8 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 5
help!
MateuszRajca at 2007-10-8 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 6

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

programmer01 at 2007-10-8 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 7

But then the contextmenustrip closes.

Matt

MateuszRajca at 2007-10-8 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 8

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

programmer01 at 2007-10-8 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 9

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

~Matt

MateuszRajca at 2007-10-8 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 10

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

programmer01 at 2007-10-8 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 11
so?
MateuszRajca at 2007-10-8 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 12

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

programmer01 at 2007-10-8 > top of Msdn Tech,Windows Forms,Windows Forms General...
# 13
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 );
}
}

Algosys at 2007-10-8 > top of Msdn Tech,Windows Forms,Windows Forms General...