简体   繁体   English

动态创建控件的问题

[英]Problems with creating controls dynamically

I'm having some issues with creating labels dynamically. 动态创建标签时遇到一些问题。 I'm trying to create a 15x15 grid with an "X" label per cell, and the code is sort of working, it creates the first label but not the others, I've tried debugging the code with breakpoints and it's calling the Controls.Add the right amount of times but it's only creating one label. 我正在尝试创建一个每个单元格带有“ X”标签的15x15网格,并且代码可以正常工作,它创建了第一个标签,但没有其他标签,我尝试了使用断点调试代码,并调用了Controls.Add正确的次数,但只创建一个标签。 Here's the code: 这是代码:

class PlayingGrid
{
    const int MAX_CELLS = 15;
    Cell[,] grids = new Cell[MAX_CELLS, MAX_CELLS];

    public PlayingGrid()
    {
        for (int y = 1; y < MAX_CELLS; y++)
        {
            int yPoint = y * 12;

            for (int x = 1; x < MAX_CELLS; x++)
            {
                int xPoint = x * 12;
                grids[y, x] = new Cell(new Point(xPoint, yPoint));
            }
        }
    }
}
class Cell
{
    #region Fields & Properties
    public Letter Letter;
    public Point Point;
    public static GameForm refForm;
    #endregion

    //Default constructor will create an empty cell
    public Cell(Point point)
    {
        Letter = new Letter(LatinAlphabet.Empty);
        this.Point = point;           
        refForm.Invoke(new Action(()=> refForm.Controls.Add(new Label() { Text = "X", Location = point })));
    }
}

Set label AutoSize property to true: 将标签的AutoSize属性设置为true:

new Label() {AutoSize =true, Text = "X", Location = point }

Because with default size all label hided in below of last label. 因为使用默认大小,所有标签都隐藏在上一个标签的下方。

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

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