-1

I am new to C# and I am trying to check which one of seven radio button within a group box is checked. The line with the "case" is giving me trouble. I am able to make it work using multiple if statements, but I would like to know if a switch statement would work too. Thanks for your help! Here is the code snippet:

      {
          if (OptMaennlich.Checked == true)
          {
              double Grundumsatz = (double)NumGewicht.Value * 24;
              switch (GrpAktivitaet)
              {
                  case OptSchlafen.Checked == true
                      double PAL = 1.0;
                      break;

1 Answers1

1

If you are still stuck and you are using Windows Forms, I would suggest subscribing to the CheckedChanged event either in the properties inspector or in the code behind for each of the radio buttons you have in your groupbox. Switching on the Text Property of the RadioButton is one way of achieving this in the event.

In your initialisation code:

OptSchlafen.CheckedChanged += SomeFunctionToCheckChangedState;

and in your code a function:

private void SomeFunctionToCheckChangedState(object sender, EventArgs e)
{
    // Insert your check logic here
    switch((sender as RadioButton).Text)
    {
        case "radiobuttontext":
            //what you want to do goes here
        break;
    }
}

When you click the radio button this event will be fired and the function called.

Dharman
  • 30,962
  • 25
  • 85
  • 135
SkullBocks
  • 11
  • 2