我有五个控件 
TextBox1;TextBox2;TextBox3;TextBox4;TextBox5;
我想通过循环赋值
for(int i=0;i=4;i++)
{
     ....=i.ToString();
}
实现:TextBox1.Text=0;TextBox2.Text=1;TextBox3.Text=2;TextBox4.Text=3;TextBox5.Text=4请问在windows窗体程序中如何实现

解决方案 »

  1.   

    给它们一个父控件,然后用foreach或for循环出来.
      

  2.   

    我的问题实际是这样的
    有一个文本控件名称叫TextBox1
    有一个字符串变量 strText = "TextBox1"那么如何能通过strText还改变TextBox1的属性呢?
      

  3.   

    private void button1_Click(object sender, System.EventArgs e)
    {
    foreach(Control con in this.Controls)
    {
    TextBox text = con as TextBox;
    if( null == text)
    continue;

    text.Text = "test";
    }
    }
      

  4.   

    sorry,没看清private void button1_Click(object sender, System.EventArgs e)
    {
    foreach(Control con in this.Controls)
    {
    if(con.Name == "textBox1")
    {
    TextBox text = con as TextBox;
    if( null == text)
    continue; text.Text = "test";
    }
    }
    }
      

  5.   

    楼上的这种方法我试过了,如果控件多了,效率很差,
    应当有更好的方法吧,就象javascript中的eval一样,反射是不是能解决这个问题呢?
      

  6.   

    private void Button1_Click(object sender, EventArgs MyEventArgs)
    {
          // Find control on page.
          Control myControl1 = FindControl(strText);
          if(myControl1!=null)
          {
             // modify your text box's properties here
          }
          else
          {
             Response.Write("Control not found");
          }
    }
      

  7.   

    谢谢楼上的,请问在windows窗体程序中如何实现呢
      

  8.   

    System.Windows.Forms.Control crl可以解决了啦for(int i=0;i<crl.count;i++)
    {
         if(crl.controls[i].name="你要的控件")
         {
              TextBox txt=crl.Controls[i] as TextBox;
               //下面就不要我说了吧?
     
              
         }
    }