|
-
October 24th, 2011, 09:44 AM
#1
If Statement w/Message Box
I'm trying to get the message box to show if the two text boxes are left blank or their value is less than 0, I can't seem to get the message box to show up and even with a negative number it calculates any way instead of the message showing up. W/o any info (left blank) the program freezes =/
//declare variables
double dblFat;
double dblCarbs;
double dblFatCalories;
double dblCarbCalories;
double dblTotalCalories;
//assign values
dblFat = Double.Parse(txtFat.Text);
dblCarbs = Double.Parse(txtCarb.Text);
//Calculate Amount of Fat/Carb calories
dblFatCalories = dblFat * 9;
dblCarbCalories = dblCarbs * 4;
dblTotalCalories = dblFatCalories + dblCarbCalories;
//Assign results to label
txtFat.Text = Convert.ToString(dblFat);
txtCarb.Text = Convert.ToString(dblCarbs);
lblCarbCaloriesValue.Text = Convert.ToString(dblCarbCalories);
lblFatCaloriesValue.Text = Convert.ToString(dblFatCalories);
if (lblFatCaloriesValue.Text == "" || ((lblCarbCaloriesValue.Text == "") || (dblTotalCalories <= 0)))
{
MessageBox.Show("Please enter a valid number", "Missing Information", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
-
October 24th, 2011, 10:05 AM
#2
Re: If Statement w/Message Box
I'm running VS2010, sorry for not stating before
-
October 24th, 2011, 10:18 AM
#3
Re: If Statement w/Message Box
Code:
//declare variables
double dblFat;
double dblCarbs;
double dblFatCalories;
double dblCarbCalories;
double dblTotalCalories;
//assign values
dblFat = Double.Parse(txtFat.Text);
dblCarbs = Double.Parse(txtCarb.Text);
//Calculate Amount of Fat/Carb calories
dblFatCalories = dblFat * 9;
dblCarbCalories = dblCarbs * 4;
dblTotalCalories = dblFatCalories + dblCarbCalories;
//Assign results to label
txtFat.Text = Convert.ToString(dblFat);
txtCarb.Text = Convert.ToString(dblCarbs);
lblCarbCaloriesValue.Text = Convert.ToString(dblCarbCalories);
lblFatCaloriesValue.Text = Convert.ToString(dblFatCalories);
if (lblFatCaloriesValue.Text == "" || ((lblCarbCaloriesValue.Text == "") || (dblTotalCalories <= 0)))
{
MessageBox.Show("Please enter a valid number", "Missing Information", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
Reply With Quote
Have you set a breakpoint at the beginning of the function and single-stepped through the code to see what is happening?
Developing using:
.NET3.5 / VS 2010
-
October 24th, 2011, 10:28 AM
#4
Re: If Statement w/Message Box
for some odd reason it seems to be hanging at the the code under //assign values
I've moved my IF statement up to make sure the program checks that first after the //assign value codes but now I'm getting an error saying that my dblFatCalories is unassigned lol
-
October 24th, 2011, 10:37 AM
#5
Re: If Statement w/Message Box
the statement works fine and continues to calculate as long as values are entered (message box pops up if a negative # is entered and does not calculate further) but if left blank it just freezes the program
-
October 24th, 2011, 12:06 PM
#6
Re: If Statement w/Message Box
The operation Double.Parse(txtFat.Text); is failing because the text is invalid.
It would be good if you had used the try{ } catch {} around that operation so you could have a messagebox in the catch block to tell you that the input is invalid.
You could also initialize the double variables to 0.0 when you declare them, then check the textbox text for "". If the text is "", then don't do the conversion; this will then use the initialized value of 0.0 for the double variables.
Last edited by Jim_Auricman; October 24th, 2011 at 01:35 PM.
Developing using:
.NET3.5 / VS 2010
-
October 24th, 2011, 09:30 PM
#7
Re: If Statement w/Message Box
To add on to the previous posters suggestions instead of using Double.Parse(); you could use Double.TryParse();
Code:
//assign values
Double.TryParse(txtFat.Text, out dblFat);
Double.TryParse(txtCarb.Text, out dblCarbs);
By doing the above it will return a value of 0 if the result is not a valid. So say the user writes 12.92 it will run correctly. Now say a field is left blank or they enter 94.23% since the second contains an invalid character (%) and the first one is blank it will return a value of 0 and prevent your program from crashing.
As mentioned above you could also check the text boxes and make sure they are filled in before doing anything
Code:
// Check that text boxes contain something inside them
if (txtText1.Text == "" || txtText2.Text == "")
{
// Make some code run here like making a message box pop up telling the user to fill all fields
}
else
{
// If fields are filled in then run your calculating code here
}
Also from what I've read it's good practice to initialize your variables when you create them to prevent errors.
Code:
double dblFat = 0.0;
double dblCarbs = 0.0;
double dblFatCalories = 0.0;
double dblCarbCalories = 0.0;
double dblTotalCalories = 0.0;
-
October 25th, 2011, 05:14 PM
#8
Re: If Statement w/Message Box
 Originally Posted by Jim_Auricman
It would be good if you had used the try{ } catch {} around that operation so you could have a
Or, a much better and less lazy solution; don't allow invalid input in the first place.
If you liked my post go ahead and give me an upvote so that my epee.... ahem, reputation will grow.
Yes; I have a blog too - http://the-angry-gorilla.com/
-
October 26th, 2011, 02:53 PM
#9
Re: If Statement w/Message Box
thanks for all the input, i'll give them a try tonight.
it's an intro class so I don't really know anything more complicated lol.
-
October 27th, 2011, 01:30 AM
#10
Re: If Statement w/Message Box
I guess you would be performing the above operation on a button click event. Just add the validations in place
In case if both the text boxes/any one of the text boxes are left empty. Just add some validation code and prompt to the user saying 'Please enter text for Textbox 1' , 'Please enter text for TextBox2' , etc.. and all your above calculation codes should be executed only when both the text box inputs are successful inputs
-
November 8th, 2011, 11:12 AM
#11
Re: If Statement w/Message Box
The if statement is checking the labels, not the textboxes - was this intentional?
Code:
if (lblFatCaloriesValue.Text == "" || ((lblCarbCaloriesValue.Text == "") || (dblTotalCalories <= 0)))
{
MessageBox.Show("Please enter a valid number", "Missing Information", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|