动态添加的控件不会显示在表单上

时间:2018-08-14 12:37:39

标签: c# .net winforms

我将控件添加到我的窗体中,该控件由数组中的位置指定。我尝试过将面板切换为按钮,但控件仍未显示。

int[,] gamefield = new int[9, 8];
Panel[,] vis_gamefield = new Panel[9, 8];

private void Real_Move(int col) 
    {
        for (int i = 6; i > 0; i--)
        {
            gamefield[col, i] = 1;
            vis_gamefield[col, i] = new Panel();
            vis_gamefield[col, i].Name = "Panel" + moves;
            vis_gamefield[col, i].BackColor = Color.Red;
            vis_gamefield[col, i].Size = new Size(88, 88);
            vis_gamefield[col, i].Location = new Point(158 + 100 * (i - 1), 174 + 99 * (col - 1));
            vis_gamefield[col, i].Visible = true;
            vis_gamefield[col, i].BringToFront();
            vis_gamefield[col, i].Show();
            Win_Check(col, i);
            moves++;
            break;
        }

    }

1 个答案:

答案 0 :(得分:2)

尚不清楚您如何将vis_gamefield添加到Forms控件集合中?

您是否有一行在Form构造函数中显示类似于 this.Controls.Add(vis_gamefield); 的行?

它应该看起来像下面的代码

public Form1()         
{
    InitializeComponent(); 
    this.Controls.Add(vis_gamefield);
}