In: Computer Science
3. In the handler code for the OK button, first check (as you did in Exercise 1 X) to ensure that the name is not blank. If the name is blank generate a message as requested in Exercise 1 X, reset focus and have the user try again to enter a non-blank name.
4. Once a non-blank name has been entered, dynamically (in your own code) enable the Enter Subtotal label and the Enter Subtotal text box.
5. Now in your handler code for btnCalculate, insert try-catch code (see p. 193 for an example) to catch an invalid decimal value entry and ask the user to re-enter his or her data.
6. Test your project and if the code you have added does not catch a negative input value, make sure you correct this problem and retest your project.
This is the code that I have.
namespace InvoiceTotal
{
public partial class frmInvoiceTotal : Form
{
public frmInvoiceTotal()
{
InitializeComponent();
}
int numberOfInvoices = 0;
decimal totalOfInvoices = 0m;
decimal invoiceAverage = 0m;
decimal largestInvoice = 0m;
decimal smallestInvoice =
Decimal.MaxValue;
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;
largestInvoice =
Math.Max(largestInvoice, invoiceTotal);
smallestInvoice
= Math.Min(smallestInvoice, invoiceTotal);
txtNumberOfInvoices.Text = numberOfInvoices.ToString();
txtTotalOfInvoices.Text = totalOfInvoices.ToString("c");
txtInvoiceAverage.Text = invoiceAverage.ToString("c");
txtLargestInvoice.Text = largestInvoice.ToString("c");
txtSmallestInvoice.Text = smallestInvoice.ToString("c");
txtEnterSubtotal.Text = "";
txtEnterSubtotal.Focus();
}
private void
btnClearTotals_Click(object sender, System.EventArgs e)
{
numberOfInvoices
= 0;
totalOfInvoices
= 0m;
invoiceAverage =
0m;
largestInvoice =
0m;
smallestInvoice
= Decimal.MaxValue;
txtNumberOfInvoices.Text = "";
txtTotalOfInvoices.Text = "";
txtInvoiceAverage.Text = "";
txtLargestInvoice.Text = "";
txtSmallestInvoice.Text = "";
txtEnterSubtotal.Focus();
}
private void
btnExit_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
Check the below code:
int numberOfInvoices = 0;
decimal totalOfInvoices = 0m;
decimal invoiceAverage = 0m;
decimal largestInvoice = 0m;
decimal smallestInvoice = Decimal.MaxValue;
private void button1_Click(object sender, EventArgs e)
{
if (textBox1.Text=="")
{
MessageBox.Show("Name is null, please enter the name");
}
else
{
// display sub total names and values
}
}
private void button2_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;
try
{
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;
largestInvoice = Math.Max(largestInvoice, invoiceTotal);
smallestInvoice = Math.Min(smallestInvoice, invoiceTotal);
txtNumberOfInvoices.Text = numberOfInvoices.ToString();
txtTotalOfInvoices.Text = totalOfInvoices.ToString("c");
txtInvoiceAverage.Text = invoiceAverage.ToString("c");
txtLargestInvoice.Text = largestInvoice.ToString("c");
txtSmallestInvoice.Text = smallestInvoice.ToString("c");
txtEnterSubtotal.Text = "";
txtEnterSubtotal.Focus();
}
catch
{
MessageBox.Show("Invalid data, renter the values");
}
}
private void button3_Click(object sender, EventArgs e)
{
numberOfInvoices = 0;
totalOfInvoices = 0m;
invoiceAverage = 0m;
largestInvoice = 0m;
smallestInvoice = Decimal.MaxValue;
txtNumberOfInvoices.Text = "";
txtTotalOfInvoices.Text = "";
txtInvoiceAverage.Text = "";
txtLargestInvoice.Text = "";
txtSmallestInvoice.Text = "";
txtEnterSubtotal.Focus();
}
private void button4_Click(object sender, EventArgs e)
{
this.Close();
}
Waiting for your valuable comments