【问题标题】:Creating same size cells with TableLayoutPanel使用 TableLayoutPanel 创建相同大小的单元格
【发布时间】:2016-09-21 18:11:24
【问题描述】:

我正在尝试在 C# 中使用 TableLayoutPanel 来填写 FormTableLayoutPanel 应该包含 10x10 的面板,所有面板都具有相同的大小(按百分比)。 虽然我似乎没有让它适用于最后一行或最后一列。

allPanel.RowCount = 10;
allPanel.ColumnCount = 10;
allPanel.Padding = 10;
allPanel.BackColor = Color.Green;
allPanel.AutoSize = true;
allPanel.Dock = DockStyle.Fill;

allPanel.RowStyles.Clear();
allPanel.ColumnStyles.Clear();

windowsForm.Controls.Add(allPanel);

for (int i = 0; i < 10; i++) {
 allPanel.ColumnStyles.Add(new ColumnStyle(SizeType.Percent, 10));
}

for (int i = 0; i < 10; i++) {
 allPanel.RowStyles.Add(new RowStyle(SizeType.Percent, 10));
}

for (int i = 0; i < 10; ++i) {
 for (int j = 0; j < 10; ++j) {
  boardTiles[i, j] = new Panel();
  boardTiles[i, j].BackColor = Color.White;
  allPanel.Controls.Add(boardTiles[i, j], i, j);
 }
}

结果如下:

【问题讨论】:

    标签: c# winforms layout tablelayoutpanel


    【解决方案1】:

    Cells' 尺寸为 integers;因此,要使布局正常工作,您需要确保 TLP 的净面积实际上 整除 可被您希望它包含的单元格数量。

    净面积是ClientSize 减去Padding

    因此,所有边的Padding10,对于n x mn x m 单元格,您需要(n * w + 20, m * h + 20) 的大小Width wHeight h

    由于您要填充容器,因此您需要:

    • 控制容器大小以匹配公式
    • 或计算 Padding 以纠正整数除法错误

    这是一个计算正确Padding的函数:

    Padding GetCorrectionPadding(TableLayoutPanel TLP, int minimumPadding)
    {
        int minPad = minimumPadding;
        Rectangle netRect = TLP.ClientRectangle;
        netRect.Inflate(-minPad, -minPad);
    
        int w = netRect.Width / TLP.ColumnCount;
        int h = netRect.Height / TLP.RowCount;
    
        int deltaX = (netRect.Width - w * TLP.ColumnCount) / 2;
        int deltaY = (netRect.Height - h * TLP.RowCount) / 2;
    
        int OddX = (netRect.Width - w * TLP.ColumnCount) % 2;
        int OddY = (netRect.Height - h * TLP.RowCount) % 2;
    
        return new Padding(minPad + deltaX, minPad + deltaY,
                           minPad + deltaX + OddX, minPad + deltaY + OddY);
    }
    

    注意代码..

    • 假设TLP 已被填满
    • 假设您想要的最小值 Padding 有一些价值。由于我们需要最多 n-1 像素来进行校正,因此水平和垂直填充可能会相差 一半,在您的情况下最多可能相差 4 or 5 像素。

    你可以这样称呼它:

    allPanel.Padding = GetCorrectionPadding(allPanel, 5);
    

    如果您想避免这种情况,您需要选择选项一,即确保容器具有合适的尺寸!

    当然,每次调整大小后都需要再次应用修正 Padding..

    【讨论】:

      【解决方案2】:

      我不喜欢 TableLayoutPanel。我可以用下面的代码实现相同的目标。 TableLayoutPanel 上的属性数量是有限的,我经常发现我需要额外的属性。您可以继承任何类(我使用按钮)。我将按钮放在表单上,​​但您也可以将按钮放在标准面板上,这样面板就可以在表单上移动,所有按钮一起移动。

      using System;
      using System.Collections.Generic;
      using System.ComponentModel;
      using System.Data;
      using System.Drawing;
      using System.Linq;
      using System.Text;
      using System.Windows.Forms;
      
      namespace Buttons
      {
          public partial class Form1 : Form
          {
              const int ROWS = 5;
              const int COLS = 10;
              public Form1()
              {
                  InitializeComponent();
                  this.Load += new System.EventHandler(this.Form1_Load);
              }
              public void Form1_Load(object sender, EventArgs e)
              {
                  new MyButton(ROWS, COLS, this);
              }
      
      
          }
          public class MyButton : Button
          {
              const int WIDTH = 50;
              const int HEIGHT = 50;
              const int SPACE = 5;
              const int BORDER = 20;
      
              public static List<List<MyButton>> buttons { get; set; }
              public static List<MyButton> buttonList { get; set; }
              public Form1 form1;
              public int row { get; set; }
              public int col { get; set; }
              public MyButton()
              {
              }
              public MyButton(int rows, int cols, Form1 form1)
              {
                  buttons = new List<List<MyButton>>();
                  buttonList = new List<MyButton>();
      
                  this.form1 = form1;
                  for(int row = 0; row < rows; row++)
                  {
                      List<MyButton> newRow = new List<MyButton>();
                      buttons.Add(newRow);
                      for (int col = 0; col < cols; col++)
                      {
                          MyButton newButton = new MyButton();
                          newButton.Height = HEIGHT;
                          newButton.Width = WIDTH;
                          newButton.Top = row * (HEIGHT + SPACE) + BORDER;
                          newButton.Left = col * (WIDTH + SPACE) + BORDER;
                          newButton.row = row;
                          newButton.col = col;
                          newRow.Add(newButton);
                          buttonList.Add(newButton);
                          newButton.Click += new System.EventHandler(Button_Click);
                          form1.Controls.Add(newButton);
                      }
                  }
              }
              public void Button_Click(object sender, EventArgs e)
              {
                  MyButton button = sender as MyButton;
                  MessageBox.Show(string.Format("Pressed Button Row {0} Column {1}", button.row, button.col));
      
              }
      
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2015-09-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多