Moving the toolbar a little too far left will create a new line if the other toolbar is on the same toolbar

When I drag the toolbar to the left (perhaps just to get it in the corner) using another toolbar in the same toolbar, the one I drag jumps to the “new” line, as if I were moving it down. This is pretty hard to explain, so here are a couple of diagrams.

Diagram A: I move the toolbar to the left and “accidentally” go too far to the left (well, only a couple of dozen pixels that users can easily make).

Chart B: This happens when the dragged toolbar falls down.

enter image description here

? (: , ).

LayoutStyle .

+5
3

, . , . , , , - ToolStripPanel, , ToolStrip OnLocationChanged ( LocationChanged, , d ToolStrip).

public class ToolStripEx : ToolStrip
{
    protected override void OnLocationChanged(EventArgs e)
    {
        if (this.Location.Y >= this.Parent.MaximumSize.Height)
        {
            this.Location = new Point(this.Location.X, 0);
        }
        else
        {
            base.OnLocationChanged(e);
        }           
    }
}


. ToolStrip , - , reset , , , .

, , ToolStrip , . , .

:

Windows Forms. ToolStripEx.cs , , :

Orientation

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


, ToolStripEx .

ToolStripContainer , Dock Fill, ..

ToolStripEx ( cog) TopToolStripPanel. .

Form1.cs:

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


. ( , Orientation of Orientation.Vertical). , , else if parent.Orientation == Orientation.Vertical.

, . , ToolStripEx .

, ToolStrip , , .

- ToolStripContainer, ToolStripEx. , - ToolStrip, ToolStripEx.

- : , , . ToolStripEx:

protected override void OnBeginDrag(EventArgs e)
{
    //base.OnBeginDrag(e);
}

, . , , , ToolStrip OnBeginDragDrop, ToolStrip , .

+1

, OnMouseLeave OnMouseEnter/OnMouseUp/etc?

public class ToolStripContainerFix : ToolStripContainer
{
    public ToolStripContainerFix()
        : base()
    {
        this.TopToolStripPanel.MouseLeave += TopToolStripPanel_MouseLeave;
        this.TopToolStripPanel.MouseEnter += TopToolStripPanel_MouseEnter;
    }

    void TopToolStripPanel_MouseLeave(object sender, EventArgs e)
    {
        this.TopToolStripPanel.SuspendLayout();
    }

    void TopToolStripPanel_MouseEnter(object sender, EventArgs e)
    {
        this.TopToolStripPanel.ResumeLayout();
    }
}

, , ... , .

+1

Winforms . .NET 2.0, Winforms . WPF. . , - . , .

. , MaximumSize :

    public Form1() {
        InitializeComponent();
        toolStripContainer1.TopToolStripPanel.MaximumSize =
            new Size(0, toolStrip1.Height);
    }

, , . .

Remember that this is not a real problem, the location of the tools is the choice of the user, and you should not help in any way. You can count on the user ordering them logically.

+1
source

All Articles