我的winform有15个textBox,其Name有规律,依次为textBox1,textBox2,....,textBox15,
而我现在希望能够将其连续清空,而不是一个一个的去clear,我用的程序如下:for (int i = 1; i <= 15; i++)
            {
                foreach (Control c in this.Controls)
                {
                    if (c.Name == "textBox" + i.ToString())
                    {
                        ((TextBox)c).Text = "";
                    }
                }
            }
可是执行后却没有什么反应,这是为什么呢?该如何写呢?

解决方案 »

  1.   

     if (c.Name == "textBox" + i.ToString())
    =======
     if (c is TextBox && c.ID == "textBox" + i.ToString())
      

  2.   

    for (int i = 1; i <= 15; i++)
                {
                    foreach (Control c in this.Controls)
                    {
                        if (c is TextBox && c.Name.Equals("textBox" + i))
                        {
                        }
                    }
                }
    或 this.Controls("textBox" + i) as TextBox
      

  3.   

    其实更多时候我会问我们的程序员:怎么会有15个TextBox?一个一个设计上去不累吗?为什么不写一个控件然后自动(依据数据绑定)部署产生一堆?
      

  4.   

                    foreach (Control c in this.Controls)
                    {
                        if (c.Name == "textBox" + i.ToString())
                        {
                            TextBox t =c as TextBox;
                            t.text="";
                        }
                    }
                    foreach (Control c in this.Controls)
                    {
                        if (typeof(c)==TextBox)//这里具体写法可能不对,不过思路是只要是textbox,就清空
                        {
                            TextBox t =c as TextBox;
                            t.text="";
                        }
                    }
      

  5.   

    虽然有更好的写法,但楼主的写法貌似的确没错。
    你的textbox是不是名字修改过了,不是默认的名字了?
    如果你想实现清空。上面的方法都不错。或是指定一下
    TextBox[] textboxArray = new TextBox[]
    {
        textBox1,
        textBox2,
        textBox3,
        textBox4,
        textBox5,
        textBox6,
        textBox7,
        textBox8,
        textBox9,
        textBox10,
        textBox11,
        textBox12,
        textBox13,
        textBox14,
        textBox15};
    foreach (TextBox t in textboxArray)
    {
        t.Text = "";
    }
      

  6.   

    可以把这15个textbox放到数组里面去。
     private TextBox[] tbs = { textBox1, textBox2, textBox3,…… };就不用一个控件一个控件的去判断了。
    for (int i = 0; i < 15; i++)
        tbs[i].Text = string.Empty;
      

  7.   

    动态绘制,绘制后放到arraylist里,然后就方便了
      

  8.   

    灵异事件,我换了一个项目可以,但是这个项目就是不行,哎,不知道该说啥子了,就用笨点的方法吧,我检查过了,textBox名字没有问题 , 是默认的。。
    谢谢各位!