1

我正在开发一个 Windows 窗体应用程序,该应用程序主要由一个具有 8 列和 9 行的 TableLayoutPanel 组成。我已经能够像这样填充每个单元格:

for (int row = 0; row < TableLayoutPanel.RowCount; row++) {
    for (int column = 0; column < TableLayoutPanel.ColumnCount; column++) {
        PictureBox pictureBox = new PictureBox();
        pictureBox.BackColor = Color.Blue;
        TableLayoutPanel.Controls.Add(pictureBox, column, row);
        pictureBox.Dock = DockStyle.Fill;
        pictureBox.Margin = new Padding(1);
}

但是,此方法从顶部开始,而不是从左到右向下,例如:

1 2 3
4 5 6

我的目标是填充 TableLayoutPanel,如:

6 5 4
1 2 3

我不知道这是否可能,但有没有办法以这种方式填充 TableLayoutPanel 单元格?

4

2 回答 2

1

别担心,伙计们,我想出了如何做到这一点。我所做的是从底部的一行TableLayoutPanel(在我的情况下是 row = 8)开始,然后从那里开始工作。然后我确定行值是奇数还是偶数,在这种情况下我会改变方向。这是解决方案。

for (int row = TableLayoutPanel.RowCount-1; row >= 0; row--) {
    if (row % 2 == 0) { //if even
        for (int column = 0; column < TableLayoutPanel.ColumnCount; column++) {
            PictureBox pictureBox = new PictureBox();
            pictureBox.BackColor = Color.Blue;
            TableLayoutPanel.Controls.Add(pictureBox, column, row);
            pictureBox.Dock = DockStyle.Fill;
            pictureBox.Margin = new Padding(1);
    } else { 
        for (int column = TableLayoutPanel.ColumnCount-1; column >= 0; column--) {
            PictureBox pictureBox = new PictureBox();
            pictureBox.BackColor = Color.Blue;
            TableLayoutPanel.Controls.Add(pictureBox, column, row);
            pictureBox.Dock = DockStyle.Fill;
            pictureBox.Margin = new Padding(1);
    }
}
于 2013-05-17T08:06:02.877 回答
0

尝试将您的 for 循环替换为:

for (int row = TableLayoutPanel.RowCount - 1, c0 = 0, c1 = TableLayoutPanel.ColumnCount - 1, cs = 1; row >= 0 ; row--, c0 ^= c1, c1 ^= c0, c0 ^= c1, cs *= -1)
{
    for (int column = c0; column != c1 + cs; column += cs)
    {
         ...
于 2013-05-15T14:58:45.423 回答