views:

285

answers:

4

I have have some code which adds new cells to a table and fills them with text boxes.

The way I've coded it so far works fine:

        TableCell tCell1 = new TableCell();
        TableCell tCell2 = new TableCell();
        TableCell tCell3 = new TableCell();
        TableCell tCell4 = new TableCell();
        TableCell tCell5 = new TableCell();
        TableCell tCell6 = new TableCell();
        TableCell tCell7 = new TableCell();

        TextBox txt1 = new TextBox();
        TextBox txt2 = new TextBox();
        TextBox txt3 = new TextBox();
        TextBox txt4 = new TextBox();
        TextBox txt5 = new TextBox();
        TextBox txt6 = new TextBox();
        TextBox txt7 = new TextBox();

        tCell1.Controls.Add(txt1);
        tCell2.Controls.Add(txt2);
        tCell3.Controls.Add(txt3);
        tCell4.Controls.Add(txt4);
        tCell5.Controls.Add(txt5);
        tCell6.Controls.Add(txt6);
        tCell7.Controls.Add(txt7);

        tRow.Cells.Add(tCell1);
        tRow.Cells.Add(tCell2);
        tRow.Cells.Add(tCell3);
        tRow.Cells.Add(tCell4);
        tRow.Cells.Add(tCell5);
        tRow.Cells.Add(tCell6);
        tRow.Cells.Add(tCell7);

As you can see there's basically 4 instructions getting repeated 7 times. I'm sure there has to be a way to accomplish this with just 4 lines of code within a FOR loop and having all the names dynamically assigned but I just can't seem to find anything that would point me in the direction of how to do it.

Something like the following is what I'm after:

    for (int i = 0; i < 6; i++)
    {

        TableCell tCell[i] = new TableCell();
        TextBox txt[i] = new TextBox();
        tCell[i].Controls.Add(txt[i]);
        tRow.Cells.Add(tCell[i]);

    }

Any help would be much appreciated.

A: 

This should work fine?

for (int i = 0; i < 6; i++)
{
    TableCell tCell = new TableCell();
    TextBox txt = new TextBox();
    tCell.Controls.Add(txt);
    tRow.Cells.Add(tCell);
}

I don't really get what you need the names for though.
Do you plan on using the "txt5" name as a reference to that specific textbox?
Why not just use tRow.Cells[4].Controls[0] As TextBox ?

Lars Mæhlum
+2  A: 

I think this should do it:

    for (int i = 0; i < 7; i++)
    {

        TableCell tCell = new TableCell();
        TextBox txt = new TextBox();
        tCell.Controls.Add(txt);
        tRow.Cells.Add(tCell);

    }

Make sure that 6 is changed to a 7.

Eugene Katz
A: 

What you wrote actually looks pretty close to me. There a re a few points to keep in mind though.

I don't believe you need the array index. As long as tRow is initialized outside the loop it will add the new elements each time. You also may want to set the ID property of each textbox so you can access any specific on you may be looking for down the road.

Ian Jacobs
A: 

Hi Guys,

Thanks for all the helpful answers. To those asking questions about what I was doing with the arrays, I wasn't! That was just an example of what I was trying to achieve.

Ian and Lars got the right idea in the fact that I will need to refer to these textboxes later so I just need to use Eugene and Lubos' solution and make sure I'm adding a line that will give them sequential ID's (txt1, txt2 etc) so that I can do this.

Thanks again for all the wonderful (and quick!) input, I'm now in love with this site!

Jay Wilde