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

时间:2016-05-24 13:29:13

标签: c# winforms layout tablelayoutpanel

我正在尝试使用C#中的new_x, new_y = zip(*sorted(zip(x, y))) 来填写TableLayoutPanelForm shoud持有10x10面板,它们都具有相同的大小(按百分比计)。 虽然我似乎没有让它适用于最后一行或最后一列。

我的代码到目前为止:

TableLayoutPanel

结果如下:

enter image description here

也许你们有解决这个问题的方法。

2 个答案:

答案 0 :(得分:1)

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

净面积是ClientSize减去Padding

因此,如果所有方面的Padding 10 (n * w + 20, m * h + 20) n x mWidth w小区的Height h大小为Padding }}

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

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

这是一个计算正确的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

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

当然,每次调整大小后都需要再次应用更正{{1}}。

答案 1 :(得分:0)

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

        }

    }
}
相关问题