c#/ winforms:两个控件如何动态共享可用空间?

时间:2021-08-01 19:51:20

How would I setup my controls for the following situation?:

如何针对以下情况设置控件?:

I have a parent-container for example a GroupBox.

我有一个父容器,例如GroupBox。

Inside this parent-container I have two similar controls, like for example ListBoxes, next to each other. They both have the same size, so the border between the two of them is exactly in the middle of the GroupBox.

在这个父容器中,我有两个相似的控件,例如ListBoxes,彼此相邻。它们都具有相同的大小,因此它们之间的边界恰好位于GroupBox的中间。

Now when the GroupBox is resized, I want the ListBoxes to also be resized, but the two should always be at the same size than the other one. So also the border between the two of them stays in the middle of the GroupBox.

现在,当GroupBox被调整大小时,我希望ListBox也可以调整大小,但是两者应该总是与另一个大小相同。因此,它们之间的边界也保持在GroupBox的中间。

So, how would I set up the properties for these three controls to achieve my desired behaviours?

那么,我如何设置这三个控件的属性以实现我想要的行为?

3 个解决方案

#1


You need another container. The TableLayoutPanel is the best solution. Use 1 row and 2 columns and dock (Dock = Fill) it in the group box. The width of both columns should be set to 50%. Next you can add your controls in the individual cells and dock them (Dock = Fill)

你需要另一个容器。 TableLayoutPanel是最好的解决方案。使用1行和2列并在组框中停靠(Dock = Fill)它。两列的宽度应设置为50%。接下来,您可以在单个单元格中添加控件并停靠它们(Dock = Fill)

#2


Perhaps a SplitContainer with the two-halves set evenly and IsSplitterFixed set to true (to stop the user moving it):

也许SplitContainer将两半均匀设置并且IsSplitterFixed设置为true(以阻止用户移动它):

[STAThread]
static void Main() {
    Application.EnableVisualStyles();
    Application.Run(new Form {
        Controls = { new SplitContainer {
            Width = 200,
            IsSplitterFixed = true,
            SplitterDistance = 100,
            SplitterWidth = 1,
            Dock = DockStyle.Fill,
            Panel1 = { Controls = {
                    new ListBox {
                        IntegralHeight = false,
                        Dock = DockStyle.Fill,
                        BackColor = Color.Blue,
                        Items = {"abc","def","ghi"}
                    }
                }
            }, Panel2 = { Controls = {
                    new ListBox {
                        Dock = DockStyle.Fill,
                        BackColor = Color.Red,
                        IntegralHeight = false,
                        Items = {"jkl","mno","pqr"}
                    }
                }
            }
        }}
    });        
}

#3


It is also possible without the splitcontainer.

没有拆分容器也可以。

In the resize event of the groupbox set the location of the first control to {0,0} and of the second to {GroupBox.Width/2,0}, and set the sizes of both to {GroupBox.Width/2, GroupBox.Height}

在groupbox的resize事件中,将第一个控件的位置设置为{0,0},将第二个控件的位置设置为{GroupBox.Width / 2,0},并将两者的大小设置为{GroupBox.Width / 2,GroupBox 。高度}

You should also leave space around the controls so they don't overlap with the border of the GroupBox.

您还应该在控件周围留出空间,这样它们就不会与GroupBox的边框重叠。

private void groupBox1_Resize(object sender, EventArgs e)
{
    groupBox1.SuspendLayout();

    listBox1.Location = new Point(7, 20);
    listBox2.Location = new Point(groupBox1.Width / 2, 20);

    listBox1.Size = new Size(groupBox1.Width / 2 - 6, groupBox1.Height - 27);
    listBox2.Size = new Size((groupBox1.Width + 1) / 2 - 6, groupBox1.Height - 27);

    groupBox1.ResumeLayout();
}

#1


You need another container. The TableLayoutPanel is the best solution. Use 1 row and 2 columns and dock (Dock = Fill) it in the group box. The width of both columns should be set to 50%. Next you can add your controls in the individual cells and dock them (Dock = Fill)

你需要另一个容器。 TableLayoutPanel是最好的解决方案。使用1行和2列并在组框中停靠(Dock = Fill)它。两列的宽度应设置为50%。接下来,您可以在单个单元格中添加控件并停靠它们(Dock = Fill)

#2


Perhaps a SplitContainer with the two-halves set evenly and IsSplitterFixed set to true (to stop the user moving it):

也许SplitContainer将两半均匀设置并且IsSplitterFixed设置为true(以阻止用户移动它):

[STAThread]
static void Main() {
    Application.EnableVisualStyles();
    Application.Run(new Form {
        Controls = { new SplitContainer {
            Width = 200,
            IsSplitterFixed = true,
            SplitterDistance = 100,
            SplitterWidth = 1,
            Dock = DockStyle.Fill,
            Panel1 = { Controls = {
                    new ListBox {
                        IntegralHeight = false,
                        Dock = DockStyle.Fill,
                        BackColor = Color.Blue,
                        Items = {"abc","def","ghi"}
                    }
                }
            }, Panel2 = { Controls = {
                    new ListBox {
                        Dock = DockStyle.Fill,
                        BackColor = Color.Red,
                        IntegralHeight = false,
                        Items = {"jkl","mno","pqr"}
                    }
                }
            }
        }}
    });        
}

#3


It is also possible without the splitcontainer.

没有拆分容器也可以。

In the resize event of the groupbox set the location of the first control to {0,0} and of the second to {GroupBox.Width/2,0}, and set the sizes of both to {GroupBox.Width/2, GroupBox.Height}

在groupbox的resize事件中,将第一个控件的位置设置为{0,0},将第二个控件的位置设置为{GroupBox.Width / 2,0},并将两者的大小设置为{GroupBox.Width / 2,GroupBox 。高度}

You should also leave space around the controls so they don't overlap with the border of the GroupBox.

您还应该在控件周围留出空间,这样它们就不会与GroupBox的边框重叠。

private void groupBox1_Resize(object sender, EventArgs e)
{
    groupBox1.SuspendLayout();

    listBox1.Location = new Point(7, 20);
    listBox2.Location = new Point(groupBox1.Width / 2, 20);

    listBox1.Size = new Size(groupBox1.Width / 2 - 6, groupBox1.Height - 27);
    listBox2.Size = new Size((groupBox1.Width + 1) / 2 - 6, groupBox1.Height - 27);

    groupBox1.ResumeLayout();
}