Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I am working on an Visual Studio application and i am trying to figure out how to add a Try Catch block to my code

I am working on an Visual Studio application and i am trying to figure out how to add a Try Catch block to my code so that it would catch any negative inputs that are entered by a user. The code at the bottom is the current code that i have and im not sure how to add the try catch block to this section of code "private void btnCalculate_Click(object sender, EventArgs e)"

namespace InvoiceTotal

{

public partial class frmInvoiceTotal : Form

{

public frmInvoiceTotal()

{

InitializeComponent();

}

int numberOfInvoices = 0;

decimal totalOfInvoices = 0m;

decimal invoiceAverage = 0m;

decimal SmallestInvoice = 0m;

decimal LargestInvoice = 0m;

private void btnCalculate_Click(object sender, EventArgs e)

{

decimal subtotal = Convert.ToDecimal(txtEnterSubtotal.Text);

decimal discountPercent = .25m;

decimal discountAmount = Math.Round(subtotal * discountPercent, 2);

decimal invoiceTotal = subtotal - discountAmount;

txtSubtotal.Text = subtotal.ToString("c");

txtDiscountPercent.Text = discountPercent.ToString("p1");

txtDiscountAmount.Text = discountAmount.ToString("c");

txtTotal.Text = invoiceTotal.ToString("c");

numberOfInvoices++;

totalOfInvoices += invoiceTotal;

invoiceAverage = totalOfInvoices / numberOfInvoices;

txtNumberOfInvoices.Text = numberOfInvoices.ToString();

txtTotalOfInvoices.Text = totalOfInvoices.ToString("c");

txtInvoiceAverage.Text = invoiceAverage.ToString("c");

if (numberOfInvoices == 1)

{

SmallestInvoice = invoiceTotal;

LargestInvoice = invoiceTotal;

}

else

{

SmallestInvoice = Math.Min(SmallestInvoice, invoiceTotal);

LargestInvoice = Math.Max(LargestInvoice, invoiceTotal);

}

txtSmallestInvoice.Text = SmallestInvoice.ToString("c");

txtLargestInvoice.Text = LargestInvoice.ToString("c");

txtEnterSubtotal.Text = " ";

txtSubtotal.Focus();

}

private void btnExit_Click(object sender, EventArgs e)

{

this.Close();

}

private void btnClearTools_Click(object sender, EventArgs e)

{

numberOfInvoices = 0;

totalOfInvoices = 0m;

invoiceAverage = 0m;

txtNumberOfInvoices.Text = "";

txtTotalOfInvoices.Text = "";

txtInvoiceAverage.Text = "";

txtEnterSubtotal.Focus();

}

private void frmInvoiceTotal_Load(object sender, EventArgs e)

{

}

private void btnOk_Click(object sender, EventArgs e)

{

if (txtName.Text.Trim() == string.Empty)

{

MessageBox.Show("Please enter a valid name!");

return;

}

else

{

txtEnterSubtotal.Enabled = true;

}

}

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Lab Manual For Database Development

Authors: Rachelle Reese

1st Custom Edition

1256741736, 978-1256741732

More Books

Students also viewed these Databases questions

Question

=+ 2. What is the opportunity cost of seeing a movie?

Answered: 1 week ago

Question

=+ what roles should government play in them ?

Answered: 1 week ago

Question

=+ Why do we have markets and, according to economists,

Answered: 1 week ago