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

时间:2016-10-06 08:22:15

标签: c# winforms tablelayoutpanel

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

enter image description here

这是我的代码:

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

我尝试将按钮缩小,并增加了表单大小,但最后一列始终小于前两列。我无法理解!

2 个答案:

答案 0 :(得分:1)

首先清除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;

...

答案 1 :(得分:1)

Reza Aghaei向我指出了这个线索How to create a magic square using Windows Forms?,它指出了我正确的方向。下面更新(和工作)代码。 :)

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