944,204 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Unsolved
  • Views: 3549
  • C# RSS
May 24th, 2006
0

Cannot Test For Condition

Expand Post »
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:

C# Syntax (Toggle Plain Text)
  1. if (textBox.Text == "")
  2. labelResult = "Please enter a temperature to convert.";
  3. else
  4. if ((radiobutton1.Checked == false) && (radiobutton.Checked == false)
  5. labelResult = "Please select a conversion type to perform":
  6. else
  7. 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?
Similar Threads
Reputation Points: 10
Solved Threads: 2
Posting Whiz in Training
Toulinwoek is offline Offline
274 posts
since Mar 2005
May 24th, 2006
0

Re: Cannot Test For Condition

try this:
C# Syntax (Toggle Plain Text)
  1. if (textBox.Text == "")
  2. labelResult = "Please enter a temperature to convert.";
  3. else{
  4. if (!radiobutton1.Checked && !radiobutton.Checked)
  5. labelResult = "Please select a conversion type to perform";
  6. else
  7. calculateMethod();
  8. }
Reputation Points: 14
Solved Threads: 19
Posting Pro in Training
campkev is offline Offline
484 posts
since Jul 2005
May 24th, 2006
0

Re: Cannot Test For Condition

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.
Reputation Points: 10
Solved Threads: 2
Posting Whiz in Training
Toulinwoek is offline Offline
274 posts
since Mar 2005
May 24th, 2006
0

Re: Cannot Test For Condition

actually, I'm not 100% sure what was wrong with it the 1st time. it shouldn't even compile the way you had it listed.
Reputation Points: 14
Solved Threads: 19
Posting Pro in Training
campkev is offline Offline
484 posts
since Jul 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: help with file header
Next Thread in C# Forum Timeline: FolderBrowserDialog problems





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC