C# WinForms SplitContainer面板工具栏

nwo49xxi  于 2023-05-07  发布在  C#
关注(0)|答案(2)|浏览(201)

WinForms .NET 6.03(C#)
在顶部停靠有传统菜单和工具栏的窗体中...
我在左侧面板中有一个拆分容器(垂直拆分器),我有一个停靠在顶部的工具栏和一个填充面板工具栏下方的Panel1的用户控件。
当我调整面板大小时,工具栏消失,用户控件在面板中上移。
这在.NET 4.6中不是问题。我现在只看到它,我在. NET6.03中构建
有解决办法吗?

k3bvogb1

k3bvogb11#

事实证明,这是一个z顺序问题。在执行过程中,toolstrip不知何故丢失了关于对接的最高条件。

mnemlml8

mnemlml82#

试试这样:
别忘了右键单击【项目】,选择【同步命名空间】,根据您的项目更新命名空间。

namespace DiscountCard.View.CustomControl
{
    public partial class CtechSplitContainer : SplitContainer
    {
        private ToolStripContainer toolStripContainer1;
        private ToolStrip toolStrip1;
        private ToolStripButton button1;

        private ToolStripContainer toolStripContainer2;
        private ToolStrip toolStrip2;
        private ToolStripButton button2;

        public CtechSplitContainer()
        {
            InitializeComponent();

            toolStripContainer1 = new ToolStripContainer();
            toolStrip1 = new ToolStrip();
            button1 = new ToolStripButton();

            button1.Text = "🗖";
            button1.Click += Button1_Click;
            button1.DoubleClick += Button1_DoubleClick;

            // Add items to the ToolStrip.
            ToolStripItem[] toolStripItem1 = new ToolStripItem[] { button1 };
            toolStrip1.Items.AddRange(toolStripItem1);
            toolStrip1.LayoutStyle = ToolStripLayoutStyle.Table;
            toolStrip1.BackColor = Color.White;
            toolStrip1.Dock = DockStyle.Top;

            // Add the ToolStrip to the top panel of the ToolStripContainer.
            toolStripContainer1.TopToolStripPanel.Controls.Add(toolStrip1);
            toolStripContainer1.Dock = DockStyle.Right;
            toolStripContainer1.Height = button1.Width;
            toolStripContainer1.Width = button1.Width;

            // Add the ToolStripContainer to the Panel1.
            this.Panel1.Controls.Add(toolStripContainer1);
            this.Panel1.BackColor = Color.White;

            toolStripContainer2 = new ToolStripContainer();
            toolStrip2 = new ToolStrip();
            button2 = new ToolStripButton();

            button2.Text = "🗖";
            button2.Click += Button2_Click;
            button2.DoubleClick += Button2_DoubleClick;

            // Add items to the ToolStrip.
            ToolStripItem[] toolStripItem2 = new ToolStripItem[] { button2 };
            toolStrip2.Items.AddRange(toolStripItem2);
            toolStrip2.LayoutStyle = ToolStripLayoutStyle.Table;
            toolStrip2.Dock = DockStyle.Top;

            // Add the ToolStrip to the top panel of the ToolStripContainer.
            toolStripContainer2.TopToolStripPanel.Controls.Add(toolStrip2);
            toolStripContainer2.Dock = DockStyle.Right;
            toolStripContainer2.Height = button2.Width;
            toolStripContainer2.Width = button2.Width;
            toolStrip2.BackColor = Color.White;

            // Add the ToolStripContainer to the Panel2.
            this.Panel2.Controls.Add(toolStripContainer2);
            this.Panel2.BackColor = Color.White;

            this.SplitterWidth = 1;

            if (this.Orientation == Orientation.Vertical)
            {
                this.SplitterDistance = this.ClientSize.Width / 2;
            }
            else
            {
                this.SplitterDistance = this.ClientSize.Height / 2;
            }

            this.BackColor = Color.White;
        }

        private void Button1_Click(object? sender, EventArgs e)
        {
            if (button1.Text == "🗖")
            {
                button1.Text = "🗗";
                this.Panel1Collapsed = false;
                this.Panel2Collapsed = true;
            }
            else
            {
                button1.Text = "🗖";
                this.Panel1Collapsed = false;
                this.Panel2Collapsed = false;
            }
        }
        private void Button1_DoubleClick(object? sender, EventArgs e)
        {
            button1.PerformClick();
        }

        private void Button2_Click(object? sender, EventArgs e)
        {
            if (button2.Text == "🗖")
            {
                button2.Text = "🗗";
                this.Panel1Collapsed = true;
                this.Panel2Collapsed = false;
            }
            else
            {
                button2.Text = "🗖";
                this.Panel1Collapsed = false;
                this.Panel2Collapsed = false;
            }
        }
        private void Button2_DoubleClick(object? sender, EventArgs e)
        {
            button2.PerformClick();
        }

    }
}

结果
正常

最大化

相关问题