我一共有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++;
}
}
}
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++;
}
}
}
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;
}
}
}
}
}
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
{
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;
}
}
}
}
{
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;
}
}
}
}
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;
}
}
}
}
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;
}
}
}
}
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");
}
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++;
}
}
}