如何(创建和)向表格布局添加组件

本文关键字:表格 布局 添加 组件 创建 如何 | 更新日期: 2024-09-21 02:29:47

我正在C#中创建一个国际象棋游戏。来自Java-Swing环境的我制作了一个标准函数,该函数创建了一个8x8字段并赋予它基本属性。

     Board = new Label[8, 8];
            for (int i = 0; i < 8; i++)
            {
                for (int j = 0; j < 8; j++)
                {
                    Board[i, j] = new System.Windows.Forms.Label();
                    Board[i, j].Location = new Point(i * 50, j * 50);
                    Board[i, j].Size = new System.Drawing.Size(50, 50);
                    Board[i, j].Visible = true;

                    if ((i + j) % 2 == 0) // Color decision
                    {
                        Board[i, j].BackColor = Color.Black;
                    }
                    else {
                        Board[i, j].BackColor = Color.White;
                    }
                    this.Controls.Add(Board[i, j]);
                }
            }

现在有两个额外的数组,它们包含棋盘外缘的abc和123(这样你就可以进入类似于-"Knight to E3"的移动)。

我已经设法将所有组件添加到屏幕上,但它们目前相互重叠。我正在考虑创建一个9x9"grid-layout"并添加所有组件

Java开始,我习惯了简单的命令,比如:

    GridLayout gl = new Gridlayout(3,3);
    this.setLayout(gl);

然后所有添加的元素都会自动放入网格中。经过几个小时的研究,我在C#中找不到类似的东西。玩TableLayout只会导致更多的问题而无法解决。

我的问题是如何实现(gridlayout并将我的所有标签添加到它?我为没有发布我的任何布局代码而提前道歉,但正如我所说,它只是一团糟,没有做任何应该做的事情。

谢谢:)

如何(创建和)向表格布局添加组件

我想在Win Forms中它的工作方式有点不同。您需要创建一个TableLayoutPanel,然后访问TableLayoutPanel.Controls,并通过调用Control.ControlCollection.Add方法逐个添加新控件。

var panel = new TableLayoutPanel();
panel.ColumnCount = 3;
panel.RowCount = 4;
for(int i = 0; i< panel.ColumnCount; ++i)
    panel.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F));
for (int i = 0; i < panel.RowCount; ++i)
    panel.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F));
panel.Dock = DockStyle.Fill;
this.Controls.Add(panel);
for (int c = 0; c < 3; ++c)
{
    for (int r = 0; r < 4; ++r)
    {
        var btn = new Button();
        btn.Text = (c+r).ToString();
        btn.Dock = DockStyle.Fill;
        panel.Controls.Add(btn, c, r);
    }
}

我喜欢创建自己的类,它继承了下面代码中的表单控件。你可以添加自己的属性,比如行和列,或者用棋盘添加一个图片。请参阅下面的代码。您可以将下面的代码添加到布局面板中,而不是像我那样添加到表单中。您可以在板上创建一个继承矩形的空间,然后向您的自定义矩形添加一个图像,该图像将替换下面的按钮类。

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 WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            MyButton myButton = new MyButton(this);
        }
    }
    public class MyButton : Button
    {
        public static List<List<MyButton>> board { get; set; }
        public static List<MyButton> buttons { get; set; }
        const int WIDTH = 10;
        const int HEIGTH = 10;
        const int SPACE = 5;
        const int ROWS = 10;
        const int COLS = 20;
        public int row { get; set; }
        public int col { get; set; }
        public MyButton()
        {
        }
        public MyButton(Form1 form1)
        {
            board = new List<List<MyButton>>();
            buttons = new List<MyButton>();
            for (int _row = 0; _row < ROWS; _row++)
            {
                List<MyButton> newRow = new List<MyButton>();
                board.Add(newRow);
                for (int _col = 0; _col < COLS; _col++)
                {
                    MyButton newButton = new MyButton();
                    newButton.row = _row;
                    newButton.col = _col;
                    newButton.Width = WIDTH;
                    newButton.Height = HEIGTH;
                    newButton.Top = _row * (HEIGTH + SPACE);
                    newButton.Left = _col * (WIDTH + SPACE);
                    form1.Controls.Add(newButton);
                    newRow.Add(newButton);
                    buttons.Add(newButton);
                }
            }
        }
    }
}