我一共有9个Groupbox,每个里面都有若干的radiobutton 和 checkbox,我想要在button的click事件中,添加一段代码,让所有的radiobutton和 checkbox都恢复到未选状态,恢复之前记录所有选项的选取次数,其实就是统计每个选项的选取次数.
public  void button1_Click(object sender, EventArgs e)
 {
foreach (Control Ctr in this.Controls )
              {
                  if (Ctr is GroupBox )
                  {
                      foreach (Controls Ctrs in GroupBox)
                      {
                         if(Ctrs is RadioButton)
                         {
                             ?????
//不知道该怎么写了.                             
                      }
else if(Ctrs is CheckBox)
{
//不知道该怎么写了.
}
                  }
              }
           }
}统计的代码的时候有没有更好的办法?另外,下面这些还有没有缺陷?
 
 int[] numaccount=new int[]
 
 int a=0;
 int i = 0;      
 foreach (Control ctrl in this.groupBox2.Controls)
 {  
  if (ctrl is CheckBox) 
 {
    CheckBox cb = (CheckBox)ctrl; 
      //选中       
   if (cb.Checked)   
  { 
    
   int[i]=a+1;   
    i++;   
                 }    
            }  
else if(ctrl is radiobutton)
{
RadioButton rb = (RadioButton)ctrl;
if(rb.checked)
{
int[i]=a+1;   
    i++;   

}
          }

解决方案 »

  1.   


    public  void button1_Click(object sender, EventArgs e)
     {
    foreach (Control Ctr in this.Controls )
                  {
                      if (Ctr is GroupBox )
                      {
                          foreach (Controls Ctrs in GroupBox)
                          {
                             if(Ctrs is RadioButton)
                             {
                                 ?????
    //不知道该怎么写了.
                              (Ctrs as RadioButton).Checked=false;
                                 
                          }
    else if(Ctrs is CheckBox)
    {
    //不知道该怎么写了.
     (Ctrs as CheckBox).Checked=false;
    }
                      }
                  }
               }
    }
      

  2.   

               Dictionary<string,int> dic = new Dictionary<string,int>();
                foreach (Control c in this.groupBox1.Controls)
                {
                    if (c.GetType().ToString() == "System.Windows.Forms.CheckBox")
                    {
                        if (((CheckBox)c).Checked)
                        {
                            if (!dic.ContainsKey("CheckBox"))
                            {
                                dic["CheckBox"] = 1;
                            }
                            else
                            {
                                dic["CheckBox"] = dic["CheckBox"] + 1;
                            }
                        }
                    }
                   //
                    else if (c.GetType().ToString() == "System.Windows.Forms.RadioButton")
                    {
                        if (((RadioButton)c).Checked)
                        {
                            if (!dic.ContainsKey("RadioButton"))
                            {
                                dic["RadioButton"] = 1;
                            }
                            else
                            {
                                dic["RadioButton"] = dic["RadioButton"] + 1;
                            }
                        }
                    }
     foreach(KeyValuePair<string,int> s in dic)
     {
       Console.WriteLine(s.Key.ToString()+" Checked "+s.Value.ToString() + " Times");
     }    字典 dic 里面的结果
        RadioButton 2 //假设 2个RadioButton 被check
         CheckBox   3 //假设 3个CheckBox   被check              
      

  3.   

            public void button1_Click(object sender, EventArgs e)
            {
                foreach (Control Ctr in this.Controls)
                {
                    if (Ctr is GroupBox)
                    {
                        //Add
                        RadioButton rdoBtn = null;
                        CheckBox chk = null;
                        foreach (Controls Ctrs in GroupBox)
                        {
                            rdoBtn = Ctrs as RadioButton;
                            chk = Ctrs as CheckBox;
                            if (rdoBtn != null)
                            {
                                rdoBtn.Checked = false;
                            }
                            else if (chk != null)
                            {
                                chk.Checked = false;
                            }
                            chk=null;
                            rdoBtn = null;
                        }
                    }
                }
            }
      

  4.   

    优化一下        public void button1_Click(object sender, EventArgs e)
            {
                foreach (Control Ctr in this.Controls)
                {
                    if (Ctr is GroupBox)
                    {
                        //Add
                        RadioButton rdoBtn = null;
                        CheckBox chk = null;
                        foreach (Controls Ctrs in GroupBox)
                        {
                            rdoBtn = Ctrs as RadioButton;
                            if (rdoBtn != null)
                            {
                                rdoBtn.Checked = false;
                            }
                            else
                            {
                                chk = Ctrs as CheckBox;
                                if (chk != null)
                                {
                                    chk.Checked = false;
                                }
                            }
                            chk = null;
                            rdoBtn = null;
                        }
                    }
                }
            }
      

  5.   

    int ichk;irdtbtn;
    private void fun()
    {
    foreach (Control c in this.groupBox1.Controls) 
                { 
                    switch(c.GetType().Name)
                     {
                       case "CheckBox":
                           ichk++;
                           break
                       case "RadioButton":
                           irdtbtn++;
                           break;
                       case "GroupBox":
                           fun();
                           break;
                     }
                 }
             }
    }
      

  6.   


    int ichk;irdtbtn; 
    private void fun() 

    foreach (Control c in this.groupBox1.Controls) 
                { 
                    switch(c.GetType().Name) 
                    { 
                      case "CheckBox": 
                         if(((CheckBox)c).Checked)
                          {
                             ichk++; 
                          }
                          break 
                      case "RadioButton": 
                         if(((RadioButton)c).Checked)
                         {
                            irdtbtn++; 
                          }
                          irdtbtn++; 
                          break; 
                      case "GroupBox": 
                          fun(); 
                          break; 
                    } 
                } 
            } 
    }
      

  7.   

    Dictionary <string,int> dic = new Dictionary <string,int>(); 
                foreach (Control c in this.groupBox1.Controls) 
                { 
                    if (c.GetType().ToString() == "System.Windows.Forms.CheckBox") 
                    { 
                        if (((CheckBox)c).Checked) 
                        { 
                            if (!dic.ContainsKey("CheckBox")) 
                            { 
                                dic["CheckBox"] = 1; 
                            } 
                            else 
                            { 
                                dic["CheckBox"] = dic["CheckBox"] + 1; 
                            } 
                        } 
                    } 
                  // 
                    else if (c.GetType().ToString() == "System.Windows.Forms.RadioButton") 
                    { 
                        if (((RadioButton)c).Checked) 
                        { 
                            if (!dic.ContainsKey("RadioButton")) 
                            { 
                                dic["RadioButton"] = 1; 
                            } 
                            else 
                            { 
                                dic["RadioButton"] = dic["RadioButton"] + 1; 
                            } 
                        } 
                    } 
    }foreach(KeyValuePair <string,int> s in dic) 

      Console.WriteLine(s.Key.ToString()+" Checked "+s.Value.ToString() + " Times"); 
      

  8.   

    学习了,我是从VC++转过来的,对于C#的这个foreach总是用不好,谢谢。
      

  9.   

    调试成功的代码供你参考:
                 int i = 0;
                 foreach (Control ctrl in this.groupBox2.Controls)
                 {
                     if (ctrl is CheckBox)
                     {
                         CheckBox cb = (CheckBox)ctrl;
                         //选中       
                         if (cb.Checked)
                         {                         i++;
                         }
                     }
                     else if (ctrl is RadioButton)
                     {
                         RadioButton rb = (RadioButton)ctrl;
                         if (rb.Checked)
                         {
                             i++;
                         }
                     }
                 }
      

  10.   

    楼上的各位,所做的统计都非常好,非常感谢你们的帮助,但是可能是我没有说清楚,我最终想要得到是诸如这样的结果:在一份问卷调查中,每道题的A选项有多少人选取了,B选项有多少人选取了... 单选题是radiobutton,多选是checkbox.
      

  11.   

    sorry,我说的是你楼上的,大侠你的代码我正在看,一看就是高手,正研究呢.谢谢啊.