How to Code and Test a Windows Forms Application - The event handlers for the Invoice Total form
(Page 5 of 5 )
Figure 3-5 presents the two event handlers for the Invoice Total form. The code that’s shaded in this example is the code that’s generated when you double-click the Calculate and Exit buttons in the Form Designer. You have to enter the rest of the code yourself.
I’ll describe this code briefly here so you have a general idea of how it works. If you’re new to programming, however, you may not understand the code completely until after you read the next two chapters.
The event handler for the Click event of the Calculate button calculates the discount percent, discount amount, and invoice total based on the subtotal entered by the user. Then, it displays those calculations in the appropriate text boxes. For example, if the user enters a subtotal of $1000, the discount percent will be 20%, the discount amount will be $200, and the invoice total will be $800.
In contrast, the event handler for the Click event of the Exit button contains just one statement that executes the Close method of the form. As a result, when the user clicks this button, the form is closed, and the application ends.
In addition to the code that’s generated when you double-click the Calculate and Exit buttons, Visual Studio generates other code that’s hidden in the Designer.cs file. When the application is run, this is the code that implements the form and controls that you designed in the Form Designer. Although you may want to look at this code to see how it works, you shouldn’t modify this code with the Code Editor as it may cause problems with the Form Designer. The one exception is deleting unnecessary event wiring statements.
When you enter C# code, you must be aware of the coding rules summarized in this figure. In particular, note that each method contains a block of code that’s enclosed in braces. As you’ll see throughout this book, braces are used frequently in C# to identify blocks of code. Also, note that each statement ends with a semicolon. This is true even if the statement spans several lines of code.
You should also realize that C# is a case-sensitive language. As a result, you must use exact capitalization for all C# keywords, class names, object names, variable names, and so on. If you enter the name of a control or variable without using the correct capitalization, for example, the Code Editor won’t recognize the control or variable.
The event handlers for the Invoice Total form
private void btnCalculate_Click(object sender, System.EventArgs e)
{
decimal subtotal = Convert.ToDecimal(txtSubtotal.Text);
decimal discountPercent = 0m;
if (subtotal >= 500)
{
discountPercent = .2m;
}
else if (subtotal >= 250 && subtotal < 500)
{
discountPercent = .15m;
}
else if (subtotal >= 100 && subtotal < 250)
{
discountPercent = .1m;
}
decimal discountAmount = subtotal * discountPercent;
decimal invoiceTotal = subtotal - discountAmount;
txtDiscountPercent.Text = discountPercent.ToString("p1");
txtDiscountAmount.Text = discountAmount.ToString("c");
txtTotal.Text = invoiceTotal.ToString("c");
txtSubtotal.Focus();
}
private void btnExit_Click(object sender, System.EventArgs e)
{
this.Close();
}
Coding rules
- Use spaces to separate the words in each statement.
- Use exact capitalization for all keywords, class names, object names, variable names, etc.
- End each statement with a semicolon.
- Each block of code must be enclosed in braces ({}). That includes the block of code that defines the body of a method.
Description
- When you double-click the Calculate and Exit buttons in the Form Designer, it generates the shaded code shown above. Then, you can enter the rest of the code within the event handlers.
- The first event handler for the Invoice Total form is executed when the user clicks the Calculate button. This method calculates and displays the discount percent, discount amount, and total based on the subtotal entered by the user.
- The second event handler for the Invoice Total form is executed when the user clicks the Exit button. This method closes the form, which ends the application.
Figure 3-5 The event handlers for the Invoice Total form
Please check back next week for the continuation of this article.