Creating a dynamic TableLayout

219 Views Asked by At

I've been trying to create John Conway's game of life.

Thus I've been thinking of creating a table layout and adding buttons to each cell.

I've started the columns and rows with the layout's properly according to my desires, lets say 20x20. it's constructed with the designer.

In addition, I've created a class called Templates which contains grid-patterns which are 2-dimentional int arrays in the desired size with values of values - 0\1 *(this is just a demo, i should change the arrays type to boolean)

Then I've used the following method:

private void createButtons(TableLayoutPanel i_TableLayoutGameOfLife)
{
        for (int i = 0; i < k_GameRows; ++i)
        {
            for (int j = 0; j < k_GameColumns; ++j)
            {
                if (templates.GridPattern1[i, j] == 1)
                {
                    TableLayoutGameOfLife.SetCellPosition(new Button
                    {
                        Dock = DockStyle.Fill,
                       //  += gameButtonClicked,  //TODO: Implement
                       ForeColor = Color.White
                    }, new TableLayoutPanelCellPosition(i,j));

                  //  TableLayoutGameOfLife.GetControlFromPosition(i, j).Controls.Add(
                }
            }
        }
}

The following ran without an exception, but when I checked the amount of controls in the table I've observed it has 0 (with the debugger) controls which probably means no controls have been added to the table (I presume).

Afterwards I've been trying to update the foreground color of each position by using a similar method in such way that cells with value 1 are yellow and cells with value 0 are white by the following method:

private void updateColor(TableLayoutPanel i_TableLayoutGameOfLife)
{
    for (int i = 0; i < k_GameRows; ++i)
    {
        for (int j = 0; j < k_GameColumns; ++j)
        {
            if (templates.GridPattern1[i, j] == 1)
            {
                TableLayoutGameOfLife.GetControlFromPosition(i, j).ForeColor = Color.Yellow;
            }
            else
            {
                TableLayoutGameOfLife.GetControlFromPosition(i, j).ForeColor = Color.White;
            }
        }
    }
}

I've added the second problem as the methods are similar and as it might be bugged as well.

What might be the problem? What could be a solution?

Thanks in advance, Dan

1

There are 1 best solutions below

0
Kimon M On

I wouldn't recommend setting the controls via the 'SetCellPosition' Method.

Rather I would add the Button via the Controls and the custom Add Method. Therefore you can ensure that the TableLayout knows that there is a new Control and can properly rerender itself.

i_TableLayoutGameOfLife.Controls.Add(new Button
                    {
                        Dock = DockStyle.Fill,
                       //  += gameButtonClicked,  //TODO: Implement
                       ForeColor = Color.White
                    }, i, j);