How to use Switch Statement with RadioButtons

  • Thread starter Thread starter Joseph
  • Start date Start date
J

Joseph

I would like to know how to implement several Radiobuttons using the Switch
statement instead of If/Else conditional statement. For instance, if I have
the following:

if (RadioButton1.Checked = true)
{
//Do something;
}
elseif(RadioButton2.Checked = true)
{
//Do something else;
}
elseif(RadioButton3.Checked = true)
{
//Do something special;
}

Thanks
 
I would like to know how to implement several Radiobuttons using the Switch
statement instead of If/Else conditional statement. For instance, if I have
the following:

if (RadioButton1.Checked = true)
{
    //Do something;}

elseif(RadioButton2.Checked = true)
{
   //Do something else;}

elseif(RadioButton3.Checked = true)
{
   //Do something special;

}

Thanks

I used to have this question. My solution is rather trivial but it
worked for me. What you should do is to write a common event handler
for
all three button, e.g
// when you declare the buttons you should add
RadionButton1.CheckedChanged += new
EventHandler(radioButton_CheckedChanged);
RadionButton2.CheckedChanged += new
EventHandler(radioButton_CheckedChanged);
RadionButton3.CheckedChanged += new
EventHandler(radioButton_CheckedChanged);

private void radioButton_CheckedChanged(object sender, EventArgs e)
{
if (RadioButton1.Checked)
{
// do something
memberFunction1();
}
else if (RadioButton2.Checked)
{
// do something
memberFunction2();
}
else if (RadioButton3.Checked)
{
// do something
memberFunction3();
}
}

In my case three member function can be written as 1 member function
with three different input parameters.
Tuan
 
I used to have this question. My solution is rather trivial but it
worked for me. What you should do is to write a common event handler for
all three button, e.g
// when you declare the buttons you should add
RadionButton1.CheckedChanged += new
EventHandler(radioButton_CheckedChanged); RadionButton2.CheckedChanged
+= new
EventHandler(radioButton_CheckedChanged); RadionButton3.CheckedChanged
+= new
EventHandler(radioButton_CheckedChanged);

private void radioButton_CheckedChanged(object sender, EventArgs e) {
if (RadioButton1.Checked)
{
// do something
memberFunction1();
}
else if (RadioButton2.Checked)
{
// do something
memberFunction2();
}
else if (RadioButton3.Checked)
{
// do something
memberFunction3();
}
}

In my case three member function can be written as 1 member function
with three different input parameters. Tuan


Wouldn't the sender be the RadioButton and the switch could work on it
trivially:

switch( sender) { case RadioButton3: ... }


Personally I would go for separate events for each and remove the switch
statement, each to his own.
 
Wouldn't the sender be the RadioButton and the switch could work on it
trivially:

switch( sender) {  case RadioButton3: ... }

switch doesn't work with arbitrary types - basically just with strings
and numeric/char types. The cases have to be constants, too.
Personally I would go for separate events for each and remove the switch
statement,  each to his own.

Yes, I'd agree with that.

Jon
 
Ken Foskey said:
Wouldn't the sender be the RadioButton and the switch could work on it
trivially:

switch( sender) { case RadioButton3: ... }


Personally I would go for separate events for each and remove the switch
statement, each to his own.

I am using this to enable and disable controls depending on which
radiobutton is pressed.
It's easy to read and maintain, if you name the buttons depending on
function.

private void radioButton_Click(object sender, EventArgs e)
{
RadioButton radioBtn = (RadioButton) sender;

currentButton = radioBtn.Name;

switch (currentButton)
{
case "onceButton":
EnableControls(true, false, false, false);
break;
case "dailyButton":
EnableControls(false, true, false, false);
break;
case "weeklyButton":
EnableControls(false, false, true, false);
break;
case "monthlyButton":
EnableControls(false, false, false, true);
break;
}
}
 
Thanks everone for replying to my question. I was able to find the solution
based on some of your answers. Here is what I came up with:

private void radioButton_Click(object sender, EventArgs e)
{
RadioButton radioBtn = (RadioButton)sender;

string currentButton = radioBtn.Name;
if (radioBtn.Checked == true)
{
switch (currentButton)
{
case "radioButton1":
MessageBox.Show("You clicked the first button");
break;
case "radioButton2":
MessageBox.Show("You clicked the second button");
break;
case "radioButton3":
MessageBox.Show("You clicked the third button");
break;
}
}
}

Rather simple solution but effective for what I need it for. I normally
develop in ASP.Net and I am somewhat surprised that ASP.Net provides a
buttonlist object that you can make use of it's Items property to enumerate
to the needed radiobutton and in a Winform this feature appears to not be an
option.
 
Joseph skrev:
Thanks everone for replying to my question. I was able to find the solution
based on some of your answers. Here is what I came up with:

private void radioButton_Click(object sender, EventArgs e)
{
RadioButton radioBtn = (RadioButton)sender;

string currentButton = radioBtn.Name;
if (radioBtn.Checked == true)
{
switch (currentButton)
{
case "radioButton1":

Personally I very often use the Tag element to make a binding between a
userinterface element an the value it represent.

If your RadioButton represent for example an enum value you store it in
the Tel element and simply extract it in the eventhandler..
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top