|
-
March 10th, 2013, 09:39 PM
#1
Try/Catch Statements
Hello there! I am fairly new to C# and I'm having a problem coding a specific assignment. I was wondering if anyone could provide a few tips on what I'm doing wrong. I'm trying to have it catch 3 different things. That there is a decimal value, that there actually is something inserted, and that the number is within range of 0 and 1000. All of these need to display a different message box also. Down below is so far what I've done. I'm able to do the valid range but it's still calculating the numbers when I click calculate. Aslo when there is no value entered it brings up the "Please enter a valid subtotal" message box instead of the "Requires Subtotal" box. Thanks!
Code:
private void btnCalculate_Click(object sender, EventArgs e)
{
try
{
decimal subtotal = Decimal.Parse(txtSubtotal.Text);
if (subtotal < 0 || subtotal > 1000)
{
MessageBox.Show("Please enter a valid range");
txtSubtotal.Focus();
}
if (txtSubtotal.Text == "")
{
MessageBox.Show("Requires Subtotal");
return false;
}
decimal discountPercent = .25m;
decimal discountAmount = subtotal * discountPercent;
decimal invoiceTotal = subtotal - discountAmount;
discountAmount = Math.Round(discountAmount, 2);
invoiceTotal = Math.Round(invoiceTotal, 2);
txtDiscountPercent.Text = discountPercent.ToString("p1");
txtDiscountAmount.Text = discountAmount.ToString();
txtTotal.Text = invoiceTotal.ToString();
txtSubtotal.Focus();
Convert.ToDecimal(txtSubtotal.Text);
}
catch (FormatException)
{
MessageBox.Show("Please enter a valid subtotal");
}
}
Last edited by DataMiser; March 13th, 2013 at 12:43 AM.
Reason: added code tags for readability
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
|