C# 无法在 TableLayoutPanel 中动态自动调整列的大小

C# Unable to dynamicly auto-size columns evenly in TableLayoutPanel

我有一个带有 TableLayoutPanel 控件的 WinForm。我的代码将检测屏幕上连接的显示器数量,为每个显示器创建一列,然后在 TableLayoutControl 的每个单独列中为每个显示器添加一个按钮,这样我就可以确保无论连接多少显示器,按钮将出现在整个表单中 "centered"。 One/two 显示器呈现得很好,但是三个显示器导致端列分布不均匀。

这是我的代码:

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

我试过缩小按钮并增加表单大小,但最后一列总是小于前两列。我看不懂!

先清除 ColumnStyles。

this.monitorLayoutPanel.ColumnStyles.Clear();

然后:

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;

...

Reza Aghaei pointed me to this thread 这为我指明了正确的方向。下面更新(和工作)代码。 :)

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