HOW DO I GET THE VALUE OF THE RADIO BUTTON?

Chintoo Semi

Total Post:135

Points:947
Posted by  Chintoo Semi
C# 
C#
 991  View(s)
Ratings:
Rate this:

Here are my options (names) for the radio button:

4

2

1

0.5

0.25

I tried using this but it's giving me an error:

multiplier = Convert.ToDouble(radioButton1.SelectedItem.ToString());

Error Message:

'System.Windows.Forms.RadioButton' does not contain a definition for 'SelectedItem' and no extension method 'SelectedItem' accepting a first argument of type 'System.Windows.Forms.RadioButton' could be found (are you missing a using directive or an assembly reference?)

How do I set the value of the multiplier based on what the user set in the radio button?

  1. Pravesh Singh

    Post:412

    Points:2888
    Re: How do I get the value of the radio button?

    Hi Chintoo,


    As said in the error message, RadioButton has no SelectedItem property. You should be getting the radiobutton text instead.

    multiplier = Convert.ToDouble(radioButton1.Text);

    If you want to check if the radiobutton is selected, use Checked property instead

    if (radioButton1.Checked)

    {

        multiplier = Convert.ToDouble(radioButton1.Text);

    }

    In your case you could use a loop

    foreach (RadioButton d in this.Controls.OfType<RadioButton>())

    {

        if (d.Checked)

        {

             multiplier = Convert.ToDouble(d.Text);

        }

    }

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!