c#怎样累加radiobutton的值(用控件组之类)?
我有50多个radioButton,每个选项有一个分值,像考试系统一样,用户选好答案点确定累计得分,但是用下面的写法太麻烦,怎样把它们改写成控件组的循环累加?就像if(r...
我有50多个 radioButton,每个选项有一个分值,像考试系统一样,用户选好答案点确定累计得分,但是用下面的写法太麻烦,怎样把它们改写成控件组的循环累加?
就像if (radioButton[i].Checked)之类的?
if (radioButton1.Checked)
i = int.Parse(radioButton1.Tag.ToString());
if (radioButton2.Checked)
i = i + int.Parse(radioButton2.Tag.ToString());
………………
………………
i = i + int.Parse(radioButton49.Tag.ToString());
if (radioButton50.Checked)
i = i + int.Parse(radioButton50.Tag.ToString());
label.Text = i.ToString();
………………
咕~~(╯﹏╰)b不明白……怎么枚举怎么转型啊…… 展开
就像if (radioButton[i].Checked)之类的?
if (radioButton1.Checked)
i = int.Parse(radioButton1.Tag.ToString());
if (radioButton2.Checked)
i = i + int.Parse(radioButton2.Tag.ToString());
………………
………………
i = i + int.Parse(radioButton49.Tag.ToString());
if (radioButton50.Checked)
i = i + int.Parse(radioButton50.Tag.ToString());
label.Text = i.ToString();
………………
咕~~(╯﹏╰)b不明白……怎么枚举怎么转型啊…… 展开
2个回答
展开全部
int total = 0;
//获取当前窗体的所有控件,若放置在容器中,则用panel.controls获取容器内的控件集合
Control.ControlCollection controls = this.Controls;
//遍历控件集合
foreach (Control control in controls)
{
//当前控件是radiobutton,并且被选中
if (control.GetType() == typeof(System.Windows.Forms.RadioButton) && ((RadioButton)control).Checked)
{
total += int.Parse(((RadioButton)control).Tag.ToString());
}
}
大概就这样吧
//获取当前窗体的所有控件,若放置在容器中,则用panel.controls获取容器内的控件集合
Control.ControlCollection controls = this.Controls;
//遍历控件集合
foreach (Control control in controls)
{
//当前控件是radiobutton,并且被选中
if (control.GetType() == typeof(System.Windows.Forms.RadioButton) && ((RadioButton)control).Checked)
{
total += int.Parse(((RadioButton)control).Tag.ToString());
}
}
大概就这样吧
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询