FREE BOOK

Chapter 3: How to code and test a Windows Forms application using C# 2008

Posted by Murach Free Book | Windows Forms December 10, 2008
In this chapter, you'll learn how to code and test a Windows Forms application. When you're done, you'll be able to develop simple applications of your own.

How IntelliSense helps you enter the code for a form

In figure 3-2, you saw how IntelliSense displays a list of the available members for a class or an object. IntelliSense can also help you select a type for the variables you declare, which you'll learn how to do in chapter 4. And it can help you use the correct syntax to call a method as shown in chapters 6 and 12.

With C# 2008, IntelliSense has been improved to help you even more as you enter the basic code for an application. In particular, IntelliSense can help you enter keywords and data types, as well as the names of variables, objects, and classes. Figure 3-5 illustrates how this works.

The first example in this figure shows the completion list that IntelliSense displays when you start to enter a new line of code. Here, because I entered the letter i, the list is positioned on the last item I used that begins with that letter. In this case, it's positioned on the if keyword. As described earlier in this chapter, you can enter as many letters as you want, and Visual Studio will select the appropriate item based on your entry. You can also scroll through the list to select an item, and you can press the Tab or Enter key to insert the item into your code.

When you select an item in a list, Visual Studio displays information about that item in a tool tip. For example, the tool tip for the if keyword indicates that there is a code snippet available for the if statement. Then, if you want to insert the code snippet, you can press the Tab key twice. You'll learn more about using code snippets later in this chapter.

The second example in this figure shows the completion list that was displayed after I inserted the if keyword and then typed a space, an opening parenthesis, and the letter s. Here, the variable named subtotal is selected since that's the last item I entered that begins with the letter S. That made it easy to enter this item into the code.

If you've used previous versions of C#, you'll appreciate these expanded IntelliSense features. For example, it's easy to forget the names you've given to items such as controls and variables, so the list that's displayed can help you locate the appropriate name. And that can help you avoid introducing errors into your code.

Although it's not shown here, C# 2008 IntelliSense also lets you see the code that's behind a list while the list is still displayed. To do that, you simply press the Ctrl key and the list becomes semi-transparent. This eliminates the frustration a lot of programmers felt when code was hidden by a completion list in previous versions of Visual Studio.

The completion list that's displayed when you enter a letter at the beginning of a line of code



Figure 3-5 How IntelliSense helps you enter the code for a form

The completion list that's displayed as you enter code within a statement



Figure 3-6 The event handlers for the Invoice Total Form

Description

  • The IntelliSense that's provided for C# 2008 lists keywords, data types, variables, objects, and classes as you type so you can enter them correctly.

  • When you highlight an item in a completion list, a tool tip is displayed with information about the item.

  • If you need to see the code behind a completion list without closing the list, press the Ctrl key. Then, the list becomes semi-transparent.

The event handlers for the Invoice Total form

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 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 use IntelliSense to help enter your code, this shouldn't be a problem.

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.

Total Pages : 10 34567

comments