繁体   English   中英

使用 TableLayoutPanel 创建相同大小的单元格

[英]Creating same size cells with TableLayoutPanel

我正在尝试使用 C# 中的TableLayoutPanel来填写Form TableLayoutPanel应该包含 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);
 }
}

结果如下所示:

在此处输入图片说明

Cells的维度是integers 因此,要使布局正常工作,您需要确保TLP的净面积实际上可以被您希望它包含的单元格数整除

净面积是ClientSize减去Padding

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

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

  • 控制容器大小以匹配公式
  • 或计算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

我不喜欢 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));

        }

    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM