Visual Basic.NET
  Home arrow Visual Basic.NET arrow Page 2 - Write Readable Code and Comments for Windo...
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
VISUAL BASIC.NET

Write Readable Code and Comments for Windows Forms Applications
By: Murach Publishing
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 1
    2009-07-09

    Table of Contents:
  • Write Readable Code and Comments for Windows Forms Applications
  • The event handlers for the Invoice Total form
  • How to code with a readable style
  • How to code comments

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    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

    1. Use spaces to separate the words in each statement.
    2. To continue a statement to the next line, type a space followed by an underscore (the line-continuation character).

    Description

    1. 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.
    2. 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. 
       
    3. 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

    More Visual Basic.NET Articles
    More By Murach Publishing


     

    Buy this book now. 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.

    VISUAL BASIC.NET ARTICLES

    - User-defined Functions using Visual Basic Ap...
    - Understanding Object Binding in VBA
    - Mastering the Message Box
    - Testing a Windows Forms Application
    - Using Visual Basic.NET Features to Code a Wi...
    - Correcting Code in a Windows Forms Applicati...
    - Write Readable Code and Comments for Windows...
    - How to Code and Test a Windows Forms Applica...
    - Adding Features to a Windows Forms Applicati...
    - How to Design a Windows Forms Application
    - LINQ to XML Programming Using Visual Basic.N...
    - Understanding Delegates using Visual Basic.N...
    - Create a Sudoku Puzzle Generator using VB.NET
    - Entity Creation and Messaging in a VB.NET Te...
    - Movement and Player Statistics in a VB.NET T...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek