在循环中创建控件 [英] Creating controls within a Loop

查看:105
本文介绍了在循环中创建控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些code它增加了新的细胞来表并用文本框填充它们。

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

我有codeD的方式,至今工作正常:

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);

正如你可以看到有4基本上得到指令重复7次。我敢肯定,必须有带内只有4行code for循环,并具有所有的名字动态分配的,但我似乎无法找到任何会指向我的方向来实现这一点的一种方式如何做到这一点。

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]);

    }

任何帮助将是非常美联社preciated。

Any help would be much appreciated.

推荐答案

我觉得这个应该这样做:

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);

    }

请确保6更改为7。

这篇关于在循环中创建控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆