c# - Prevent ToolStripMenuItems from jumping to second screen -


I have an application that mostly operates through ContextMenuStrip the NotifyIcon
ToolStripMenuItems and user Can be known through them.
The problem is, when the user has two screens, the menu is jumped to the second screen when no space is available. Like so:

Image Details Enter

Here

I Can they force them to stay on the same screen? I tried to search through the web but could not find a suitable answer.

Here is a sample piece of code to test the sample:

  public partial class Form 1: Form {public Form 1 () {Arnbhiksmuh (); Var Resources = New component resource coordinator (typef (form1)); Var notifyIcon1 = new NotifyIcon (component); Var contextMenuStrip1 = New Context Menustrip (components); Var level1ToolStripMenuItem = New toolstripmenuitem ("Level 1 drop down"); Var level2ToolStripMenuItem = new toolstripmenuitem ("level 2 drop down"); Var level3ToolStripMenuItem = new toolstripmenuitem ("Level 3 drop down"); NotifyIcon1.ContextMenuStrip = contextMenuStrip1; NotifyIcon1.Icon = ((icon) (resources.GetObject ("notifyIcon1.Icon"))); NotifyIcon1. Person = true; Level2ToolStripMenuItem.DropDownItems.Add (level3ToolStripMenuItem); Level1ToolStripMenuItem.DropDownItems.Add (level2ToolStripMenuItem); ContextMenuStrip1.Items.Add (level1ToolStripMenuItem); }}  

It is not easy, but you can write code in DropDownOpening event where menus on (see its limit) on the current screen, and then ToolStripMenuItem

  private void Submenu_DropDownOpening (object sender, EventArgs e) DropDownDirection as {ToolStripMenuItem MENUITEM = from set ToolStripMenuItem; If (menuItem.HasDropDownItems == incorrect) {return; // no drop down items) // current limits of current monitor rectangle bounds = menu item. GetCurrentParent (). Bounce; Screen current screen = screen. Foramount (bounds. Location); // Watch how big our children are: int MaxWidth = 0; foreach (ToolStripMenuItem Subitem) in menuItem.DropDownItems {MaxWidth = Math.Max ​​(subitem.Width, MaxWidth); } MaxWidth + = 10; // Add to a slightly dropable room. Ent fret = Bounds. Right + MaxWide; Int PlaytimeRecreat = Current Screen. Bounce right; If (farrites> currentmonitorarite) {menuItem.DropDownDirection = ToolStripDropDownDirection.Left; } And {menuItem.DropDownDirection = ToolStripDropDownDirection.Right; In addition, make sure that you hook up the DropDownOpening event (you will actually need to add to each menu item):  
 < Code> level1ToolStripMenuItem + = submenuPropdowning;  

Comments