Write Readable Code and Comments for Windows Forms Applications - The event handlers for the Invoice Total form
(Page 2 of 4 )
Figure 3-6 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 box controls. 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.vb 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.
When you enter Visual Basic code, you must be aware of the two coding rules summarized in this figure. First, you must separate the words in each statement by one or more spaces. Note, however, that you don’t have to use spaces to separate the words from operators, although Visual Basic adds spaces for you by default. Second, if you want to continue a statement, you do that by coding a space followed by a line-continuation character, which is an underscore (_). You can see an example of that in both of the procedure declarations in this figure.
The event handlers for the Invoice Total form
Public Class frmInvoiceTotal
Private Sub btnCalculate_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnCalculate.Click
Dim discountPercent As Decimal
If txtSubtotal.Text >= 500 Then
discountPercent = 0.2
ElseIf txtSubtotal.Text >= 250 And txtSubtotal.Text < 500 Then
discountPercent = 0.15
ElseIf txtSubtotal.Text >= 100 And txtSubtotal.Text < 250 Then
discountPercent = 0.1
Else
discountPercent = 0
End If
Dim discountAmount As Decimal = txtSubtotal.Text * discountPercent
Dim invoiceTotal As Decimal = txtSubtotal.Text - discountAmount
txtDiscountPercent.Text = FormatPercent(discountPercent, 1)
txtDiscountAmount.Text = FormatCurrency(discountAmount)
txtTotal.Text = FormatCurrency(invoiceTotal)
txtSubtotal.Select()
End Sub
Private Sub btnExit_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnExit.Click
Me.Close()
End Sub
End Class
Coding rules
Use spaces to separate the words in each statement.
To continue a statement to the next line, type a space followed by an underscore (the line-continuation character).
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 procedure 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 procedure closes the form, which ends the application.
Figure 3-6. The event handlers for the Invoice Total form
Next: How to code with a readable style >>
More Visual Basic.NET Articles
More By Murach Publishing
|
This article is excerpted from chapter three of Murach's Visual Basic 2008, written by Anne Boehm (Murach, 2008; ISBN: 1890774456). Check it out today at your favorite bookstore. Buy this book now.
|
|