代码之家  ›  专栏  ›  技术社区  ›  Rawns

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

  •  2
  • Rawns  · 技术社区  · 8 年前

    我有一个WinForm,它有 TableLayoutPanel 控制我的代码将检测屏幕上连接的监视器的数量,为每个监视器创建一列,然后在 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 回复  |  直到 8 年前
        1
  •  2
  •   MSL    8 年前

    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;
    
    ...
    
        2
  •  1
  •   Community Dan Abramov    7 年前

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