我如何得到radioButtons的总和?

时间:2017-01-19 13:02:34

标签: c# string sum radio-button

我有16个单选按钮,我试图在4个案例中添加4个单选按钮的值我怎么样? 我如何得到十六个radioButton的总和?

private void button1_Click(object sender, EventArgs e)
{ 
    string ChosenMovie = "";

    if (radioButton1.Checked)
    {
        //label2.Text = "100";
        ChosenMovie = ChosenMovie + "100" + "\r\n";
    }        
    if (radioButton2.Checked)
    {
        ChosenMovie = ChosenMovie + "60" + "\r\n";
    }
    if (radioButton3.Checked)
    {
        ChosenMovie = ChosenMovie + "30" + "\r\n";
    }
    if (radioButton4.Checked)
    {
        ChosenMovie = ChosenMovie + "0" + "\r\n";
    }
    if (radioButton5.Checked)
    {
        ChosenMovie = ChosenMovie + "100" + "\r\n";
    }
    if (radioButton6.Checked)
    {
        ChosenMovie = ChosenMovie + "60" + "\r\n";
    }
    if (radioButton7.Checked)
    {
        ChosenMovie = ChosenMovie + "30" + "\r\n";
    }
    if (radioButton8.Checked)
    {
        ChosenMovie = ChosenMovie + "0" + "\r\n";
    }
    if (radioButton9.Checked)
    {
        ChosenMovie = ChosenMovie + "100" + "\r\n";
    }
    if (radioButton10.Checked)
    {
        ChosenMovie = ChosenMovie + "60" + "\r\n";
    }
    if (radioButton11.Checked)
    {
        ChosenMovie = ChosenMovie + "30" + "\r\n";
    }
    if (radioButton12.Checked)
    {
        ChosenMovie = ChosenMovie + "0" + "\r\n";
    }
    if (radioButton13.Checked)
    {
        ChosenMovie = ChosenMovie + "100" + "\r\n";
    }
    if (radioButton14.Checked)
    {
        ChosenMovie = ChosenMovie + "60" + "\r\n";
    }
    if (radioButton15.Checked)
    {
        ChosenMovie = ChosenMovie + "30" + "\r\n";
    }
    if (radioButton16.Checked)
    {
        ChosenMovie = ChosenMovie + "0" + "\r\n";     
        MessageBox.Show(ChosenMovie);
    }
}

1 个答案:

答案 0 :(得分:1)

首先,您必须确保ChosenMovieint类型。

如果要添加到ChosenMovie的号码来自您在评论专线上写的label2.Text字符串,那么您可以使用Convert.ToInt32() or int.TryParse()

...

if (radioButton1.Checked)
{
    //label2.Text = "100";

    // by Convert.ToInt32()
    ChosenMovie = ChosenMovie + Convert.ToInt32(label2.Text);

    // or, by int.TryParse()
    ChosenMovie = ChosenMovie + int.TryParse(label2.Text);
}