Click here to Skip to main content
15,892,643 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
I have multiple if - else sttments involving radiobuttons. They are used in condition part of the if - else stements. How can I convert these multiple if - else sttments into a switch sttment?
Please help.
Jyoti Bhatnagar
Posted
Comments
Sergey Alexandrovich Kryukov 10-Oct-12 18:29pm    
Why? What's the problem? What did you try so far? Any code sample?
--SA
Sergey Alexandrovich Kryukov 10-Oct-12 18:49pm    
It's simper -- alternative choice only! Please see my answer.
--SA
Jyoti5 10-Oct-12 18:52pm    
Hey SA,
I dont see ur answer. Can u please send it again.
By Mistake I deleted the comment
Thanks,
Jyoti
Sergey Alexandrovich Kryukov 10-Oct-12 18:54pm    
Refresh the page, to see it :-)
--SA
Jyoti5 11-Oct-12 18:12pm    
I have posted the code. It does not work.

You probably don't take into account the simple fact that the radio buttons work like a single choice (unlike check boxes, for example), if they are placed in the same parent control, like a panel, form (window) or a group box. When one is selected, others are always unselected. This makes the code much simpler, as you don't need your nested checks and even else if. Are you getting the idea?

And that makes writing a single case statement really simple, because there is only one case at a time.

—SA
 
Share this answer
 
All you need to do with the radio button is select the event tab under properties and select radioButton_Click under the "Checked" event. I hope this will be helpful to someone in the future.


C#
private void radioButton_Click(object sender, RoutedEventArgs e)
        {
            RadioButton radioBtn = (RadioButton)sender;
            if (radioBtn.IsChecked == true)
            {
                switch (radioBtn.Name)
                {
                    case "radioBtnName1":
                        //do something
                        break;

                    case "radioBtnName2":
                        //do something
                        break;

                    case "radioBtnName3":
                        //do something
                        break;

                    case "radioBtnName4":
                        //do something
                        break;
                }

            }


        }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900