for(int i=0;i<20;i++)
{
     string cName="textbox"+i;
     TextBox tb= this.Controls.Find(cName) as TextBox
     tb.Text=....
}

解决方案 »

  1.   


    foreach(var v in this.Controls)
    {
        TextBox tb= v as TextBox;
        if(vb!=null)
        {
            tb.Enabled=false;
        }
    }
      

  2.   

    你这个是对所有的textbox进行处理,我只想操作一部分,比较前面20个
      

  3.   

    不是对所有的textbox,而是前面的一部分如1-20个
      

  4.   


    for(int i=0;i<this.Controls.Count;i++)
    {
         Control c = this.Controls[i];
         if(c is TextBox)
        {
             TextBox tb= c as TextBox;
             //......
        }
    }
      

  5.   

     this.txt = new System.Windows.Forms.TextBox[8]; 
      txt[0]=new System.Windows.Forms.TextBox(); 
      txt[1]=new System.Windows.Forms.TextBox(); 
      txt[2]=new System.Windows.Forms.TextBox(); 
      txt[3]=new System.Windows.Forms.TextBox(); 
      txt[4]=new System.Windows.Forms.TextBox(); 
      txt[5]=new System.Windows.Forms.TextBox(); 
      txt[6]=new System.Windows.Forms.TextBox(); 
      txt[7]=new System.Windows.Forms.TextBox(); 
    /// <summary>
    /// txt[0] 
    /// <summary>
     this.txt[0].Location = new System.Drawing.Point(22, 299); 
     this.txt[0].Name = "txt[0]"; 
     this.txt[0].Size = new System.Drawing.Size(108, 21); 
     this.txt[0].TabIndex = 20; 
     this.txt[0].Text = "0"; 
     this.Controls.Add(this.txt[0]); 
    /// <summary>
    /// txt[1] 
    /// <summary>
     this.txt[1].Location = new System.Drawing.Point(112, 299); 
     this.txt[1].Name = "txt[1]"; 
     this.txt[1].Size = new System.Drawing.Size(108, 21); 
     this.txt[1].TabIndex = 21; 
     this.txt[1].Text = "1"; 
     this.Controls.Add(this.txt[1]); 
    /// <summary>
    /// txt[2] 
    /// <summary>
     this.txt[2].Location = new System.Drawing.Point(202, 299); 
     this.txt[2].Name = "txt[2]"; 
     this.txt[2].Size = new System.Drawing.Size(108, 21); 
     this.txt[2].TabIndex = 22; 
     this.txt[2].Text = "2"; 
     this.Controls.Add(this.txt[2]); 
    /// <summary>
    /// txt[3] 
    /// <summary>
     this.txt[3].Location = new System.Drawing.Point(292, 299); 
     this.txt[3].Name = "txt[3]"; 
     this.txt[3].Size = new System.Drawing.Size(108, 21); 
     this.txt[3].TabIndex = 23; 
     this.txt[3].Text = "3"; 
     this.Controls.Add(this.txt[3]); 
    /// <summary>
    /// txt[4] 
    /// <summary>
     this.txt[4].Location = new System.Drawing.Point(382, 299); 
     this.txt[4].Name = "txt[4]"; 
     this.txt[4].Size = new System.Drawing.Size(108, 21); 
     this.txt[4].TabIndex = 24; 
     this.txt[4].Text = "4"; 
     this.Controls.Add(this.txt[4]); 
    /// <summary>
    /// txt[5] 
    /// <summary>
     this.txt[5].Location = new System.Drawing.Point(472, 299); 
     this.txt[5].Name = "txt[5]"; 
     this.txt[5].Size = new System.Drawing.Size(108, 21); 
     this.txt[5].TabIndex = 25; 
     this.txt[5].Text = "5"; 
     this.Controls.Add(this.txt[5]); 
    /// <summary>
    /// txt[6] 
    /// <summary>
     this.txt[6].Location = new System.Drawing.Point(562, 299); 
     this.txt[6].Name = "txt[6]"; 
     this.txt[6].Size = new System.Drawing.Size(108, 21); 
     this.txt[6].TabIndex = 26; 
     this.txt[6].Text = "6"; 
     this.Controls.Add(this.txt[6]); 
    /// <summary>
    /// txt[7] 
    /// <summary>
     this.txt[7].Location = new System.Drawing.Point(652, 299); 
     this.txt[7].Name = "txt[7]"; 
     this.txt[7].Size = new System.Drawing.Size(108, 21); 
     this.txt[7].TabIndex = 27; 
     this.txt[7].Text = "7"; 
     this.Controls.Add(this.txt[7]); 
      private System.Windows.Forms.TextBox[] txt; 
      

  6.   

    //对所有的TextBox操作
    this.Controls.Cast<Control>().Where(x => x is TextBox).ToList().ForEach(new Action<Control>(x => x.Enabled = false));//对前20个TextBox操作
    Enumerable.Range(0, 20).Select(x => this.Controls.Cast<Control>().Where(y => y is TextBox).ToList()[x]).ToList().ForEach(new Action<Control>(k => k.Enabled = false));
      

  7.   

    你这个是对所有的textbox进行处理,我只想操作一部分,比较前面20个
    string cName="textbox"+i;
    只会找txtbox0 .....txtbox19 这20个啊
      

  8.   

     
    你这个是对所有的textbox进行处理,我只想操作一部分,比较前面20个
    string cName="textbox"+i;
    只会找txtbox0 .....txtbox19 这20个啊
    20修改下不就可以了么 ?还是LZ纠结是什么问题?