| | |
Cannot Test For Condition
Please support our C# advertiser: Intel Parallel Studio Home
![]() |
I'm developing a practice program that simply converts a temperature value from fahrenheit to celsius when the user enters an integer, selects a radio button and then clicks a button.
I'm having trouble testing if the user has selected a radio button. In the click event method for the calculate button, an if statement tests if the text box is empty and displays a message in a result label if it is (this works). If there is a valid entry, then a second if statement checks if neither of the radio buttons is checked, and if that is the case, it should display a message to that effect in the result label. This is not working.
If no radio button is selected, the program does nothing. Here's the code:
The code compiles, but again, it's as if the second condition is not being tested. I can't tell if the problem is with the second "if" expression, or of the code block is not written correctly. I could just set one of the radio buttons to be automatically selected when the program runs, but I thinkI may need to perform this kind of task in another, future situation, so I want to know what I'm doing/not doing.
Any ideas?
I'm having trouble testing if the user has selected a radio button. In the click event method for the calculate button, an if statement tests if the text box is empty and displays a message in a result label if it is (this works). If there is a valid entry, then a second if statement checks if neither of the radio buttons is checked, and if that is the case, it should display a message to that effect in the result label. This is not working.
If no radio button is selected, the program does nothing. Here's the code:
C# Syntax (Toggle Plain Text)
if (textBox.Text == "") labelResult = "Please enter a temperature to convert."; else if ((radiobutton1.Checked == false) && (radiobutton.Checked == false) labelResult = "Please select a conversion type to perform": else calculateMethod();
The code compiles, but again, it's as if the second condition is not being tested. I can't tell if the problem is with the second "if" expression, or of the code block is not written correctly. I could just set one of the radio buttons to be automatically selected when the program runs, but I thinkI may need to perform this kind of task in another, future situation, so I want to know what I'm doing/not doing.
Any ideas?
:!: In The Beginning, God; In The End, God. In between, believe whatever you like. :)
•
•
Join Date: Jul 2005
Posts: 483
Reputation:
Solved Threads: 19
try this:
C# Syntax (Toggle Plain Text)
if (textBox.Text == "") labelResult = "Please enter a temperature to convert."; else{ if (!radiobutton1.Checked && !radiobutton.Checked) labelResult = "Please select a conversion type to perform"; else calculateMethod(); }
Awesome! That worked.
I didn't know I could use the "not" operator (!) in that kind of situation. That simplifies a lot of things I had been thinking about for some other concepts.
Also, after studyng this code, I think can see why the second "if-else" statements needed to be bracketed. The example in the book I'm studying with was using recursive "if" statements to test the same condition against multiple possibilities; in this case, I'm testing two different conditions. Am I understanding this correctly?
At any rate, thanks for the solution. I guess I was trying to make it more complex than it needed to be.
I didn't know I could use the "not" operator (!) in that kind of situation. That simplifies a lot of things I had been thinking about for some other concepts.
Also, after studyng this code, I think can see why the second "if-else" statements needed to be bracketed. The example in the book I'm studying with was using recursive "if" statements to test the same condition against multiple possibilities; in this case, I'm testing two different conditions. Am I understanding this correctly?
At any rate, thanks for the solution. I guess I was trying to make it more complex than it needed to be.
:!: In The Beginning, God; In The End, God. In between, believe whatever you like. :)
![]() |
Similar Threads
- Political Test (Geeks' Lounge)
- error: non-lvalue in unary `&' (C)
- Help with Gateway Solo P3C (Troubleshooting Dead Machines)
- Loops (C++)
- how to test equality of 2 objects (C++)
- Looking for Modem test software. (Windows NT / 2000 / XP)
Other Threads in the C# Forum
- Previous Thread: help with file header
- Next Thread: FolderBrowserDialog problems
| Thread Tools | Search this Thread |
.net access algorithm array asp barchart bitmap box broadcast buttons c# check checkbox client column combobox control conversion csharp custom database datagrid datagridview dataset datetime degrees development display draganddrop drawing encryption enum equation event excel file form format formbox forms formupdate function gdi+ httpwebrequest image index input install java label linux list listbox mandelbrot math mouseclick mysql networking operator packaging parse path photoshop picturebox pixelinversion post powerpacks programming radians regex remote remoting reporting richtextbox robot server sleep socket sql statistics stream string table text textbox thread time timer transform treeview update usercontrol validation visualstudio webbrowser wfa windows winforms wpf xml





