0

I have 16 Radio Button and I am trying to add the value of 4 radio button in each of 4 cases how I can? How do I get the sum of the sixteen 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);
    }
}
Cœur
  • 37,241
  • 25
  • 195
  • 267

1 Answers1

1

First, you have to make sure that ChosenMovie is an int type.

If the number you want to add to ChosenMovie comes from a string like label2.Text that you wrote on a commented line, then you can use 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);
}
Rizki Pratama
  • 551
  • 4
  • 23