Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>There's probably a couple ways to do this. I've experimented with a few, but none are perfect. I would say the least complicated way I found is to set the maximum size of the ToolStripPanel like Hans mentioned, and also to subclass the ToolStrip and override <code>OnLocationChanged</code> (or assign an event handler to <code>LocationChanged</code> rather than subclassing, but you'd have to assign a handler to each ToolStrip).<br/></p> <pre><code>public class ToolStripEx : ToolStrip { protected override void OnLocationChanged(EventArgs e) { if (this.Location.Y &gt;= this.Parent.MaximumSize.Height) { this.Location = new Point(this.Location.X, 0); } else { base.OnLocationChanged(e); } } } </code></pre> <p><br/></p> <p><em>Note:</em> This causes the mouse to jump back and forth when trying to drag the ToolStrip down, due to the fact that the Location is reset <em>after</em> it already changed, so it is essentially moving down, then immediately jumping back up.</p> <p>It's also worth mentioning that this may be an annoyance to the user, especially if they are purposely trying to put the ToolStrip in a new row, so I wouldn't really recommend doing this. But since you asked, there it is.</p> <p><strong>Update with complete steps and code:</strong></p> <p>I created a new blank Windows Forms project. I added a new file <code>ToolStripEx.cs</code> to the solution, and this is what is inside:</p> <p><strong>Updated for the other panels and their <code>Orientation</code></strong></p> <pre><code>using System; using System.Drawing; using System.Windows.Forms; namespace WindowsFormsApplication2 { public class ToolStripEx : ToolStrip { protected override void OnLocationChanged(EventArgs e) { if (this.Parent is ToolStripPanel) { ToolStripPanel parent = this.Parent as ToolStripPanel; if (parent.Orientation == Orientation.Horizontal) { if (this.Location.Y != 0) { this.Location = new Point(this.Location.X, 0); return; } } else if (parent.Orientation == Orientation.Vertical) { if (this.Location.X != 0) { this.Location = new Point(0, this.Location.Y); return; } } } base.OnLocationChanged(e); } } } </code></pre> <p><br/> Then I built the solution so the <code>ToolStripEx</code> class will show up in the Toolbox.</p> <p>Then I put a regular <code>ToolStripContainer</code> onto the form from the Toolbox, set <code>Dock</code> to <code>Fill</code>, set the colors, etc.</p> <p>Then I dragged two <code>ToolStripEx</code>s (with the cog icon you mentioned) from the Toolbox to the <code>TopToolStripPanel</code>. I set their colors and renderers and whatnot.</p> <p>This is what <code>Form1.cs</code> looks like:</p> <p><strong>Updated to set the other maximum sizes</strong></p> <pre><code>using System.Drawing; using System.Windows.Forms; namespace WindowsFormsApplication2 { public partial class Form1 : Form { public Form1() { InitializeComponent(); this.toolStripContainer1.TopToolStripPanel.MaximumSize = new Size(0, toolStripEx1.Height); this.toolStripContainer1.LeftToolStripPanel.MaximumSize = new Size(toolStripEx1.Height, 0); this.toolStripContainer1.BottomToolStripPanel.MaximumSize = new Size(0, toolStripEx1.Height); this.toolStripContainer1.RightToolStripPanel.MaximumSize = new Size(toolStripEx1.Height, 0); } } } </code></pre> <p><br/></p> <p><em>Note</em>: This code prevents any of the panels from expanding their rows (or columns if they have an <code>Orientation</code> of <code>Orientation.Vertical</code>) If you want the side panels to be able to expand, don't set their maximum size and get rid of the <code>else if parent.Orientation == Orientation.Vertical</code> section.</p> <p>That should be all there is to it. I ran this, and both of the <code>ToolStripEx</code>s did not disappear when moving them around.</p> <p>Like Hans said, the <code>ToolStrip</code> class is pretty quirky and pretty much any solution to your problem is not going to be perfect unless you develop your own controls from the ground up with your needs in mind.</p> <p>If for some reason you need to extend the <code>ToolStripContainer</code> class, keep it separate from the new <code>ToolStripEx</code> class. I suspect that having the nested classes was causing you to still be using the regular <code>ToolStrip</code> rather than the <code>ToolStripEx</code> class.</p> <p><strong>Another Update - fixing the mouse jumping:</strong> I found this by accident when experimenting to get rid of the mouse cursor problem. Add this to the <code>ToolStripEx</code> class:</p> <pre><code>protected override void OnBeginDrag(EventArgs e) { //base.OnBeginDrag(e); } </code></pre> <p>Strangely enough, that seems to cut down significantly on the toolstrip's resistance to being dragged outside of a panel. I haven"t dug into why this works, but it seems like ToolStrip implements its own dragging behavior without using basic drag/drop functionality and by overriding <code>OnBeginDragDrop</code> the ToolStrip is then using its custom behavior exclusively which makes the mouse act a lot nicer when dragging it.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload