private void button1_Click(object sender, System.EventArgs e)
{   
int i=0;
MyCollection textboxCollection = new MyCollection();//定以的集合,以便foreach调用!!!!!!
textboxCollection.Add(textbox1);
textboxCollection.Add(textbox2);
textboxCollection.Add(textbox3);
textboxCollection.Add(textbox4);
textboxCollection.Add(textbox5);
textboxCollection.Add(textbox6);
MyCollection labelCollection = new MyCollection();
labelCollection.Add(label1);
labelCollection.Add(label2);
labelCollection.Add(label3);
labelCollection.Add(label4);
labelCollection.Add(label5);
labelCollection.Add(label6);
foreach(TextBox newtextbox in textboxCollection)
{  
 foreach(Label newlabel in labelCollection)

 i++;
 newtextbox.Name="textbox"+i.ToString();
 if(newtextbox.Text=="")
{
 newlabel.Name="label"+i.ToString();
 MessageBox.Show(newlabel.Text+"不能为空!!");
newtextbox.Focus();    
 return;
 }
  
 }
}
}
//代码错在那里,我一点也看不出来!!!!!!

解决方案 »

  1.   

    private void button1_Click(object sender, System.EventArgs e)
    {   
    int i=0;
    MyCollection textboxCollection = new MyCollection();//定以的集合,以便foreach调用!!!!!!
    textboxCollection.Add(textbox1);
    textboxCollection.Add(textbox2);
    textboxCollection.Add(textbox3);
    textboxCollection.Add(textbox4);
    textboxCollection.Add(textbox5);
    textboxCollection.Add(textbox6);
    MyCollection labelCollection = new MyCollection();
    labelCollection.Add(label1);
    labelCollection.Add(label2);
    labelCollection.Add(label3);
    labelCollection.Add(label4);
    labelCollection.Add(label5);
    labelCollection.Add(label6);string textBoxIsEmpty  ;
    foreach(TextBox newtextbox in textboxCollection)

    if(newtextbox.Text ==String.Empty)
    {
    textBoxIsEmpty = textBoxIsEmpty + newtextbox.Name +"\n" ;}
      
    }
    MessageBox.Show(textBoxIsEmpty + +"不能为空!!");
    return ;
    }
      

  2.   

    MessageBox.Show(textBoxIsEmpty + +"不能为空!!"); 多了个+
      

  3.   

    newtextbox.Name="textbox"+i.ToString();
    这个要这样写
    newtextbox.Name=((TextBox)(Page.FindControl("textbox"+i.ToString())).Text
    注,用Page.不一定能找到,用递归或textbox所在最内一层容器
      

  4.   

    starliu(star)我刚试过了,还是不行,谢谢你了
    xiaoqhuang 也谢谢你了,你说的是好象是asp.net,我是在Window编程中出现的问题
      

  5.   

    int j=0;
    foreach(TextBox newtextbox in textboxCollection)
    {  
     j++
     if(newtextbox.Text=="")
     {
       foreach(Label newlabel in labelCollection)
       { 
         i++;
         if(i==j)
    {
            MessageBox.Show("label"+1.ToString()+"不能为空!!");
         newtextbox.Focus();    
         return;
     }
    }
      
     }
    }
    }
      

  6.   

    newtextbox.Name="textbox"+i.ToString();
    newlabel.Name="label"+i.ToString();
    ======
    上面这两句莫名其妙, 分别对TextBox和Label控件的Name属性重新赋值了!!!??其议何为?是否这样:foreach(TextBox newtextbox in textboxCollection)
    {  
      i++;
      if(newtextbox.Text=="")
      {
         MessageBox.Show(labelCollection[i].Text+"不能为空!!");
         newtextbox.Focus();   
         return;
      }
    }对语法不太熟悉, 也没验证, 上面代码仅一思路, 错误之处请各位大侠指正
      

  7.   

    加上try catch,看看报什么错
      

  8.   

    谢谢你了philoo(小毛驴尥蹶子) 问题解决了,太谢谢你了
    同时也感谢大家对我的帮助!!!!!!!!!!!!!!!!
    foreach(TextBox newtextbox in textboxCollection)
    {  
      if(newtextbox.Text=="")
      {
         MessageBox.Show(labelCollection[i].Text+"不能为空!!");
         newtextbox.Focus();   
         return;
      }
    i++;//这个写在这就没事了,否则就会从1开始!!!!!!!!!
    }