C# 使用循环创建控件集合

C# 使用循环创建控件集合,c#,winforms,C#,Winforms,因此,我基本上尝试使用以下代码通过for循环从pictureboxes中创建一个网格: private void Form1_Load(object sender, EventArgs e) { for (int i = 0; i <= 5; i++) { for (int j = 0; j <= 5; j++) { PictureBox tile = new PictureBox() ;

因此,我基本上尝试使用以下代码通过for循环从pictureboxes中创建一个网格:

private void Form1_Load(object sender, EventArgs e)
{
    for (int i = 0; i <= 5; i++)
    {
        for (int j = 0; j <= 5; j++)
        {
            PictureBox tile = new PictureBox() ;
            tile.Size = new Size(49, 49);
            tile.BackColor = Color.Firebrick;
            tile.Location = new Point((100 + (50 * i)), (100 + (50 * j)));
            Debug.WriteLine("PB created with index ["+i+","+j+"]");
        }
    }
}
private void Form1\u加载(对象发送方,事件参数e)
{

对于(int i=0;i,您没有将刚刚创建的控件添加到表单的控件集合中

private void Form1_Load(object sender, EventArgs e)
{
    for (int i = 0; i <= 5; i++)
    {
        for (int j = 0; j <= 5; j++)
        {
            PictureBox tile = new PictureBox() ;
            tile.Size = new Size(49, 49);
            tile.BackColor = Color.Firebrick;
            tile.Location = new Point((100 + (50 * i)), (100 + (50 * j)));
            Debug.WriteLine("PB created with index ["+i+","+j+"]");

            // The control needs to be added to the form's Controls collection
            Controls.Add(tile);
        }
    }
}
private void Form1\u加载(对象发送方,事件参数e)
{

对于(int i=0;i,您没有将刚刚创建的控件添加到表单的控件集合中

private void Form1_Load(object sender, EventArgs e)
{
    for (int i = 0; i <= 5; i++)
    {
        for (int j = 0; j <= 5; j++)
        {
            PictureBox tile = new PictureBox() ;
            tile.Size = new Size(49, 49);
            tile.BackColor = Color.Firebrick;
            tile.Location = new Point((100 + (50 * i)), (100 + (50 * j)));
            Debug.WriteLine("PB created with index ["+i+","+j+"]");

            // The control needs to be added to the form's Controls collection
            Controls.Add(tile);
        }
    }
}
private void Form1\u加载(对象发送方,事件参数e)
{

对于(int i=0;我非常感谢!对于错误的结果,我深表歉意:P@IanH.不,WPF的布局引擎和控件管理的工作方式与WinForms非常不同。@Craig是的,如果我不清楚我的声明,很抱歉,我的意思是WPF在运行时添加控件而不删除旧控件要困难得多。非常感谢!对不起Noom我想:P@IanH.不,WPF的布局引擎和控件管理的工作方式与WinForms非常不同。@Craig是的,如果我不清楚我的声明,很抱歉,我的意思是WPF在运行时添加控件而不删除旧控件要困难得多。