473,320 Members | 1,572 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

How to use Switch Statement with RadioButtons

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
Jul 18 '08 #1
7 14499
On Thu, 17 Jul 2008 18:24:06 -0700, Joseph
<Jo****@discussions.microsoft.comwrote:
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:
Well, as you have probably already seen, you can't use your RadioButton
reference as the parameter for a switch() statement.

You didn't show the rest of the code, so it's hard to know what you're
actually trying to do. However, typically you'd be trying to do something
in response to changes in the button state. For that, it seems to me that
the most straight-forward way is to just handle the CheckedChanged event.
Use a different handler for each button, where each handler has the
button-specific code you desire.

Other alternatives include:

-- store a "switchable" value in the Tag property for the button
-- store a delegate specific to each button in the Tag property
-- store the button control references in an array, using the index of
each button as your "switchable" value, and simply enumerating the array
to determine the correct index (see Array.IndexOf())

If none of those ideas seem useful, you'll probably have to ask a more
specific question, with details that actually explain what you're trying
to do.

Pete
Jul 18 '08 #2
tat
On Jul 17, 8:24*pm, Joseph <Jos...@discussions.microsoft.comwrote:
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

Jul 18 '08 #3
On Fri, 18 Jul 2008 07:08:45 -0700, tat wrote:

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.
Jul 18 '08 #4
On Jul 18, 3:13*pm, Ken Foskey <rmove.fos...@optushome.com.auwrote:
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: ... }
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
Jul 18 '08 #5
TAB


"Ken Foskey" <rm**********@optushome.com.auskrev i meddelandet
news:48********@dnews.tpgi.com.au...
On Fri, 18 Jul 2008 07:08:45 -0700, tat wrote:

>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.
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;
}
}

Jul 18 '08 #6
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" wrote:
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

Jul 18 '08 #7
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..

--
Bjørn Brox
Jul 20 '08 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

35
by: Thomas Matthews | last post by:
Hi, My son is writing a program to move a character. He is using the numbers on the keypad to indicate the direction of movement: 7 8 9 4 5 6 1 2 3 Each number has a direction except...
17
by: prafulla | last post by:
Hi all, I don't have a copy of C standard at hand and so anyone of you can help me. I have always wondered how switch statements are so efficient in jumping to the right case (if any)? Can...
18
by: swaroophr | last post by:
Which of switch statement and if-else statement takes less time to execute?
13
by: PeterZ | last post by:
Hi, Back to basics! My understanding is that the only way to exit a For-Next loop prematurely is with the 'break' keyword. How are you supposed to do that if you're inside a Switch...
1
by: Berko | last post by:
I'm having problems with a switch statement that is wrapped in a foreach statement so that each index in an array is evaluated against the switch statement. The switch/case will then return some...
19
by: rdavis7408 | last post by:
Hello, I have four textboxes that the user enters the price per gallon paid at the pump, the mileage per gallon and I would like to then calculate the cost per gallon and use a switch statement to...
4
by: priyanka | last post by:
Hi there, I had a question. Is there any way of testing a string value in a switch statement. I have about 50 string values that can be in a string variable. I tried cheking them with the if...
5
by: mark4asp | last post by:
Every time the function below is called I get the alert. So I put a deliberate error in there and I check the value of (reportType=='MANDATE') in Firebug, which is found to be true. But still the...
2
by: Phillip B Oldham | last post by:
What would be the optimal/pythonic way to subject an object to a number of tests (based on the object's attributes) and redirect program flow? Say I had the following: pets = {'name':...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.