【问题标题】:Assigning each Control in a TableLayoutPanel to a 2D Array将 TableLayoutPanel 中的每个控件分配给 2D 数组
【发布时间】:2016-06-05 22:02:11
【问题描述】:

我创建了一个有 100 个插槽 (10x10) 的 TableLayoutPanel。在每个单元格中,我创建了一个新的TableLayoutPanel。 然后我创建了一个数组TableLayoutPanel[,] tb = new TableLayoutPanel[10,10];

所以现在我想遍历主 (10x10) TableLayoutPanel 的每个单元格,并将每个单元格中的每个 tablelayoutpanel 分配给 2D 数组中的内存槽。

我一直在网上进行头脑风暴和搜索,并得到暗示这将涉及两个for 循环和一个foreach 循环,但我不知道去哪里。

根据我的编码,我认为最好的分配位置是在这段代码中:

                for (int i = 0; i < 10; i++)
                {
                    for (int j = 0; j < 10; j++)
                    {
                        mainTPL.Controls.Add(new TableLayoutPanel
                        {
                            Name = "tbl" + i + "-" + j,
                            CellBorderStyle = TableLayoutPanelCellBorderStyle.Inset,
                            Size = new Size(70, 70),
                            RowCount = 2,
                            ColumnCount = 3,
                        }, i, j);
                      //Could I do the assignment right here?
                    }
                }

我没有 for each 循环,但是可以在那个双循环中进行分配吗?如何指定新创建的TableLayoutPanel 将其分配到数组中? 谢谢各位。

【问题讨论】:

    标签: c# multidimensional-array foreach controls tablelayoutpanel


    【解决方案1】:

    所以我找到了一种使用嵌套if 语句的方法。我敢打赌还有其他方法可以做到这一点,但目前这对我有用。希望这对某人有所帮助。

    TableLayoutPanel[,] grid = new TableLayoutPanel[10, 10];
        int k=0,l=0;   
         foreach (TableLayoutPanel c in mainTPL.Controls)
                            {  
                                if(k<10 && l<10)
                                {
                                     grid[k, l] = c;
                                     if (l != 9)
                                         l++;
                                     else
                                     {
                                            l = 0;
                                            k++;
                                     }
                                }
                            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-04-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-28
      • 1970-01-01
      相关资源
      最近更新 更多