winform窗体程序
我窗体上有10个textbox,依次命名为tbCity1, tbCity2......直到tbCity10现在程序里,我想循环读取读取控件值,该怎么写?
比如,我想实现大概如下的过程for (int i = 1; i <= 10; i++)
{
this.tbCity[i].....
}
其实我想通过类似这种方式来实现读取textbox的值
不过我上面写的不对,不知道正确的应该怎么写???

解决方案 »

  1.   

    TextBox[] tbs = new TextBoxs[] { tbCity1, tbCity2, ... };
    for (int i = 1; i <= 10; i++)
    {
        tbs[i].....
    }
      

  2.   


    不是这种模式,这种就是先把10个textbox放进去了,如果要是20个,30个,岂不麻烦了
    我要的是for循环里能够自动读取到的
      

  3.   

    foreach (TextBox tb in this.Controls.OfType<TextBox>().Where(x => x.Name.StartsWith("city")))
    {
        tb....
    }
      

  4.   

    for (int i = 1; i <= 10; i++)
    {
    TextBox tb=Controls["city"+i] as TextBox;
    if(tb!=null)
    {
    ......
    }
    }
      

  5.   


    这样写,不知道为什么,tb永远都是null....
      

  6.   

    TextBox tb=Controls["city"+i] as TextBox;
    改下控件名
      

  7.   

    是这样,我这些个textbox都放在一个groupbox里
      

  8.   

    foreach(control con in this.form.controls )
    {
    if(con is Textbox && con.id.contains("tbCity"))
    {
    ......
    }
    }
    我觉的大致是这个意思
      

  9.   

    如果直接在窗体上,楼上给的程序是可以读取到的
    不过我是放在groupbox里面的,就读取不到了
      

  10.   

    TextBox tb=groupbox1.Controls["tbcity"+i] as TextBox;
      

  11.   


    //遍历panel1中所有控件
    foreach (Control uc in this.panel1.Controls)
                    {
                        foreach (Control w in uc.Controls)
                        {
                            if (w.Left != 2 && w.Left != 354)
                                w.Text = string.Empty;
                        }
                    }
      

  12.   

    感谢10楼 hjywyj在你的帮助下解决了,竟然还可以这样写,呵呵,学了一招。