How to add ContextMenuStrip to menu item?
Hi!
How can I add a ContextMenuStrip to a menu item?
Matt
Hi!
How can I add a ContextMenuStrip to a menu item?
Matt
You can see this behaviour in the Windows Start Menu. Try right-clicking on entries.
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
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 :)
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 :)
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 :)
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 :)
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 );
}
}