RadioButton and switch

I have many switches, so I get code similar to this

if (rbFrenchImp.Checked) { } else if (rbFrenchMet.Checked) { } else if (rbEnglishImp.Checked) { } else if (rbFrenchEuro.Checked) { } //etc... 

So I was wondering if it is possible to use a switch with a switch? If so, how?

+4
source share
7 answers

Yes, you can:

You sign the same CheckChanged event handler (or similar) for each of the radio buttons. Then you added the following code:

 RadioButton btn = sender as RadioButton; if(btn!= null && btn.IsChecked) { Switch(btn.Name) { case "rbFrenchImpl": break; ... } } 

This works on all types of frameworks.

+6
source

It feels a bit hacky, but you can set up a Dictionary<RadioButton, Action> containing a mapping between the RadioButton controls and the corresponding methods (parameterless void methods corresponding to the subscription of the Action delegate):

 Dictionary<RadioButton, Action> _rbMethods = new Dictionary<RadioButton, Action>(); 

When loading the form, add buttons and appropriate methods to the map:

 _rbMethods.Add(rbFrenchImp, FrenchImpAction); _rbMethods.Add(rbFrenchMet, FrenchMetAction); _rbMethods.Add(rbEnglishImp, EnglishImpAction); 

Then you can easily call the appropriate methods for the selected radio button (or buttons, if they are in different groups):

 foreach (RadioButton key in _rbMethods.Keys.Where(rb => rb.Checked)) { _rbMethods[key](); } 
+4
source

Here is the best method I found for modifying the "if statement" with a few switches in the case switch. All you have to do with the switch is select the events tab in the properties and select radioButton_Click in the "Checked" section. I hope this will be useful for someone in the future.

 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; } } } 
+2
source

Do you mean RadioButtonList ? If so, you can just search for .SelectedValue .

Otherwise, if you only have a set of switches, you can choose an option, yes, a loop over them in some way. I would suggest, however, that you can probably replace the circuitry you use with RadioButtonList to succeed.

+1
source

IF you use ASP.NET, you can use radiobuttonlist and switch, as in this example dont't know about window shape or wpf

0
source
 //create a global radioCheckStatus RadioCheckStatus radioStatus = RadioCheckStatus.none; //declare your enum private enum RadioCheckStatus { none = 0, rbFrenchImp = 1, rbFrenchMet = 2, rbEnglishImp = 3, rbFrenchEuro = 4 } //On CheckedChanged event of your radioButtons set it to changed radioButtons enum private void rbFrenchImp_CheckedChanged(object sender, EventArgs e) { radioStatus = RadioCheckStatus.rbFrenchImp; } // whereever you want to check the selected value switch (radioStatus) { case RadioCheckStatus.rbFrenchImp: { break; } case RadioCheckStatus.rbFrenchEuro: { break; } case RadioCheckStatus.rbEnglishImp: { break; } case RadioCheckStatus.rbFrenchMet: { break; } default: break; } 
0
source

Here is a solution using Linq:

 var rbResult = from rb in gbState.Controls.OfType<RadioButton>() where rb.Checked select rb; 

This will give you the <IENumerable>RadioButton . Since your ham radio must be in a container like GroupBox, this IENumberable can only contain 1 or 0 elements.

I think this should work. If not, please correct me.

0
source

Source: https://habr.com/ru/post/1332350/


All Articles