locked
Switch and radiobuttons RRS feed

  • Question

  • Hey, i got yet another question with c-sharp and visual studio. I have windows form where i got 5 different radiobuttons and ok button. Once i have clicked OK button, im suppose to go in method where i have switch wich is suppose to choose on case depending on what radiobutton is chosen but i dont know how to set it work like i wanted.

     private void myChoice()
            {
                MyComparerClass MyC = new MyComparerClass();

                switch (RadioButton )         //Heres the problem, says RadioButton wrong, dont know what i should put there so it would work
                {
                    case radioButLim.Checked = true:
                        table = MyC.Comp(signal, SingleComp.EQ, complimit);
                        return (table);

                    default:
                        break;
                }
            }

    I know this is relatively simple problem but it seems i cant get it working nor find anything that would help me with it. Agen if i have given too little information, tell me and i post it as soon i can. Thanks in advance.

    Wednesday, November 24, 2010 10:06 AM

Answers

  • Here is the code for my last suggestion.

     

    private void radioButton1_CheckedChanged(object sender, EventArgs e)
            {
                if (true == radioButton1.Checked)
                {
                    radioChecked = radioButton1.Text;
                }
            }

    private void buttonOK_Click(object sender, EventArgs e)
            {
                switch (radioChecked)
                {
                    case "Item 1":
                        //Do Something;
                        break;
                    default:
                        break;
                }
            }

    • Proposed as answer by vijaykamat Wednesday, November 24, 2010 10:20 AM
    • Marked as answer by Lumikettu Wednesday, November 24, 2010 10:57 AM
    Wednesday, November 24, 2010 10:20 AM

All replies

  • You can not directly mention "RadioButton". C# is going to look for a variable in your code named "RadioButton" and that will generate error (if you have by any chance declared such a variable that C# will work with that and result in a bug)

    What you actually need if the list / array of all the radio buttons and a loop. Something line this:

     

    foreach(RadioButton radioButton in listRadioButton)

    {

                 if(true == radioButton.Checked)         //If the cuurent radio button is selected
                {

                   switch(radioButton.Text) //Assuming the text holds information on what type of button value it is.

                   {

                        case "SomeValue":
                        table = MyC.Comp(signal, SingleComp.EQ, complimit);
                        return (table);

                    default:
                        break;
                }

    }

    Wednesday, November 24, 2010 10:12 AM
  • Try this:

     

    private void button1_Click(object sender, EventArgs e)

            {

                switch (radioButton1.Checked)

                {

                    case true:

                        //do something

                        break;

                    default:

                        //do something

                        break;

                }

            }


    GandRalph the Red
    • Proposed as answer by GandRalph Wednesday, November 24, 2010 10:14 AM
    Wednesday, November 24, 2010 10:14 AM
  • There is another way to do this (which I feel is a better one)

     

    Have a variable and update it every time the radio button is checked. When you press the "Ok" button the variable is holding the latest value checked, so you can switch on that.

    Wednesday, November 24, 2010 10:14 AM
  • Here is the code for my last suggestion.

     

    private void radioButton1_CheckedChanged(object sender, EventArgs e)
            {
                if (true == radioButton1.Checked)
                {
                    radioChecked = radioButton1.Text;
                }
            }

    private void buttonOK_Click(object sender, EventArgs e)
            {
                switch (radioChecked)
                {
                    case "Item 1":
                        //Do Something;
                        break;
                    default:
                        break;
                }
            }

    • Proposed as answer by vijaykamat Wednesday, November 24, 2010 10:20 AM
    • Marked as answer by Lumikettu Wednesday, November 24, 2010 10:57 AM
    Wednesday, November 24, 2010 10:20 AM
  • hi Lumikettu

    you don't have too many items that you want to use from switch statement,

    you only have one option of task for every radioButtons thus you need only for a  if_else statement,

    use code like this:

     

      string radioChecked;
      private void radioButton1_CheckedChanged(object sender, EventArgs e)
      {
       if (radioButton1.Checked == true)
        radioChecked = radioButton1.Text;
      }
    
      private void radioButton2_CheckedChanged(object sender, EventArgs e)
      {
       if (radioButton2.Checked == true)
        radioChecked = radioButton2.Text;
      }

     



    .Net adventurer

    If (a post answers your question) {please click " Mark As Answer" and "Mark as Helpful" on that post} 

    Wednesday, November 24, 2010 10:48 AM
  • Hello vali ansari,

     

    I suggested switch as Lumikettu  says that on "ok" we need to find which button was checked and do some operation.

    The code you suggested puts the radio button's text in to the variable same as I am doing it, but no place for operation.

    If you put ok button code into the radio button itself, that will also work but doesn't fit the brief by Lumikettu.

    Wednesday, November 24, 2010 11:01 AM
  • Hello vali ansari,

     

    I suggested switch as Lumikettu  says that on "ok" we need to find which button was checked and do some operation.

    The code you suggested puts the radio button's text in to the variable same as I am doing it, but no place for operation.

    If you put ok button code into the radio button itself, that will also work but doesn't fit the brief by Lumikettu.


    excuse me vijaykamat ,

    that was a mistake from me

    i want to tell it to Lumikettu, not you.

    thanks.


    .Net adventurer

    If (a post answers your question) {please click " Mark As Answer" and "Mark as Helpful" on that post} 

    Wednesday, November 24, 2010 11:10 AM