C#无法在TableLayoutPanel中动态均匀地自动调整列大小

C#无法在TableLayoutPanel中动态均匀地自动调整列大小,c#,winforms,tablelayoutpanel,C#,Winforms,Tablelayoutpanel,我有一个WinForm,它有一个TableLayoutPanel控件。我的代码将检测屏幕上连接的监视器的数量,为每个监视器创建一列,然后在TableLayoutControl中的每个列中为每个显示添加一个按钮,因此我可以确保无论连接了多少监视器,按钮都将在整个表单中显示为“居中”。一个/两个监视器渲染得很好,但是三个监视器会导致端列分布不均匀 这是我的密码: int count = Screen.AllScreens.Count(); this.

我有一个WinForm,它有一个
TableLayoutPanel
控件。我的代码将检测屏幕上连接的监视器的数量,为每个监视器创建一列,然后在
TableLayoutControl
中的每个列中为每个显示添加一个按钮,因此我可以确保无论连接了多少监视器,按钮都将在整个表单中显示为“居中”。一个/两个监视器渲染得很好,但是三个监视器会导致端列分布不均匀

这是我的密码:

            int count = Screen.AllScreens.Count();
            this.monitorLayoutPanel.ColumnCount = count;

            ColumnStyle cs = new ColumnStyle(SizeType.Percent, 100 / count);
            this.monitorLayoutPanel.ColumnStyles.Add(cs);

            this.monitorLayoutPanel.AutoSize = true;

            var buttonSize = new Size(95, 75);

            int z = 0;
            foreach (var screen in Screen.AllScreens.OrderBy(i => i.Bounds.X))
            {

                Button monitor = new Button
                {
                    Name = "Monitor" + screen,
                    AutoSize = true,
                    Size = buttonSize,

                    BackgroundImageLayout = ImageLayout.Stretch,                                                  
                    BackgroundImage = Properties.Resources.display_enabled,
                    TextAlign = ContentAlignment.MiddleCenter,
                    Font = new Font("Segoe UI", 10, FontStyle.Bold),
                    ForeColor = Color.White,
                    BackColor = Color.Transparent,
                    Text = screen.Bounds.Width + "x" + screen.Bounds.Height,
                    Anchor = System.Windows.Forms.AnchorStyles.None
                };


                this.monitorLayoutPanel.Controls.Add(monitor, z, 0);
                z++;
                monitor.MouseClick += new MouseEventHandler(monitor_Click);
            }

我尝试过缩小按钮,增加表单大小,但最后一列总是比前两列小。我不明白

首先清除列样式

this.monitorLayoutPanel.ColumnStyles.Clear();
然后:

int count=Screen.AllScreens.count();
for(int i=0;i
将我指向这条线,它将我指向了正确的方向。更新(和工作)代码如下:


谢谢你的建议。它使最后两列更加均匀,但所有三列仍然分布不均匀:可能是因为float参数。@MSL是的,
ColumnStyles
应该首先清除。对于这个问题,我会将按钮列表放在表单的底部中心。若要使动态控件在表单的底部中心对齐,请查看第二个链接帖子。您可以将按钮控件添加到自动调整大小的
FlowLayoutPanel
,其锚定设置为
Top,Bottom
,并位于停靠到按钮
TableLayoutPanel
的单列中。如果对内容使用合适的锚定,则单个单元格
TableLayoutPanel
会将自动调整大小的内容保持在中心(例如
None
Top,Bottom
):
int count = Screen.AllScreens.Count();

for (int i = 0; i < count; i++)
{
    ColumnStyle cs = new ColumnStyle(SizeType.Percent, (float)100 / count);
    this.monitorLayoutPanel.ColumnStyles.Add(cs);
}

this.monitorLayoutPanel.AutoSize = true;

...
            int screens = Screen.AllScreens.Count();
            this.monitorLayoutPanel.ColumnStyles.Clear();
            this.monitorLayoutPanel.ColumnCount = screens;            
            this.monitorLayoutPanel.AutoSize = true;

            int z = 0;
            foreach (var screen in Screen.AllScreens.OrderBy(i => i.Bounds.X))
            {
                var percent = 100f / screens;
                this.monitorLayoutPanel.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, percent));

                Button monitor = new Button
                {
                    Name = "Monitor" + screen,
                    Size = new Size(95, 75),
                    BackgroundImageLayout = ImageLayout.Stretch,                                                  
                    BackgroundImage = Properties.Resources.display_enabled,
                    TextAlign = ContentAlignment.MiddleCenter,
                    Font = new Font("Segoe UI", 10, FontStyle.Bold),
                    ForeColor = Color.White,
                    BackColor = Color.Transparent,
                    Text = screen.Bounds.Width + "x" + screen.Bounds.Height,
                    Anchor = System.Windows.Forms.AnchorStyles.None
                };


                this.monitorLayoutPanel.Controls.Add(monitor, z, 0);
                z++;
                monitor.MouseClick += new MouseEventHandler(monitor_Click);