如何(创建和)向表布局添加组件? [英] How to (create and) add components to a Table Layout?
问题描述
我正在用C#
创建一个国际象棋游戏.来自Java-Swing
环境,我创建了一个标准函数,该函数创建一个8x8字段并为其提供基本属性.
I am in the process of creating a chess game in C#
. Coming from a Java-Swing
environment I made a standard function that creates a field 8x8 and gives it basic attributes.
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(以便您可以输入-向E3前进的骑士" ).
Now there are two additional arrays which hold the abc's and 123's of the outer edge of the chess board (so that you can enter a move like - "Knight to E3").
我设法将所有组件添加到屏幕上,但是它们当前相互重叠.我当时正在考虑创建9x9的"grid-layout
"并向其中添加所有组件.
I've managed to add all components onto the screen but they currently overlap each other. I was thinking of creating a 9x9 "grid-layout
" and adding all components to it.
在Java
中,我习惯于简单的命令,例如:
From Java
I am used to simple commands like:
GridLayout gl = new Gridlayout(3,3);
this.setLayout(gl);
然后所有添加的元素都会自动放入网格中.
经过许多小时的研究,我在C#
中找不到任何类似的内容.玩TableLayout
只会引起更多的问题,即可以解决的问题.
And then all added elements get automatically put into the grid.
After many hours of research, I cannot find anything similar in C#
. Playing with the TableLayout
only causes more problems that solutions.
我的问题是如何实现(grid
)layout
并为其添加所有标签?
我为未发布我的布局代码深表歉意,但是就像我说的那样,这只是一团糟,什么也不做.
My question is how to implement a (grid
) layout
and add all my labels to it?
I apologize in advanced for not posting any of my Layout code, but like I said it is just a mess and does nothing that it should.
谢谢:)
推荐答案
我喜欢创建自己的类,该类继承下面的代码等表单控件.您可以添加自己的属性,例如row和col,或者使用国际象棋棋盘添加图像,即棋子的图片.请参阅下面的代码.您可以将下面的代码添加到布局面板中,而不是像我一样添加到表单中.您可以在板上创建一个继承矩形的空间,然后将图像添加到自定义矩形中,以替换下面的按钮类.
I like creating my own class that inherits a form control like code below. You can add your own properties like row and col or with a chess board an image which is a picture of the piece. See code below. You can add the code below to a layout panel instead of adding to a form like I did. You can create a space on the board which inherits a rectangle and then add an image to your custom rectangle which would replace the button class below.
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);
}
}
}
}
}
这篇关于如何(创建和)向表布局添加组件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!