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 to work with the Text Editor toolbar

Figure 3-10 shows how you can use the Text Editor toolbar to work with code. If you experiment with this toolbar, you'll find that its buttons provide some useful functions for working with comments and indentation and for moving from one place to another.

In particular, you can use the Text Editor toolbar to modify several lines of code at once. For example, during testing, you can use this toolbar to comment out several lines of code by selecting the lines of code and then clicking on the Comment button. Then, you can test the program without those lines of code. If necessary, you can use the Uncomment button to restore those lines of code. Similarly, you can use the Increase Indent and Decrease Indent buttons to adjust the indentation for selected lines of code.

You can also use the Text Editor toolbar to work with bookmarks. After you use the Toggle Bookmark button to mark lines of code, you can easily move between the marked lines of code by using the Next and Previous buttons. Although you usually don't need bookmarks when you're working with simple applications like the one shown here, bookmarks can be helpful when you're working with applications that contain more than a few pages of code.

If you experiment with the other buttons on the Text Editor toolbar, you'll find that they provide IntelliSense features like the ones you learned about earlier in this chapter for referring to properties, methods, and events. You can use these buttons to force Visual Studio to display a member list or information about a member that's displayed in the Code Editor.

How to collapse or expand blocks of code

As you write the code for an application, you may want to collapse or expand some of the regions, comments, and methods to make it easier to scroll through the code and locate specific sections of code. To do that, you can use the techniques described in figure 3-10. In this figure, for example, the frmInvoiceTotal method has been collapsed so all you can see is its method declaration.

You may also want to collapse or expand code before you print it. Then, in the dialog box for the File Print command, you can check or uncheck the Hide Collapsed Regions box. If this box is checked, Visual Studio will only print the code that's displayed in the Code Editor.

The Code Editor and the Text Editor toolbar



Figure 3-8 How to use the Text Editor toolbar and collapse or expand code


How to use the buttons of the Text Editor toolbar

  • To display or hide the Text Editor toolbar, right-click in the toolbar area and choose Text Editor from the shortcut menu.

  • To comment or uncomment several lines of code, select the lines and click the Comment or Uncomment button. During testing, you can comment out coding lines so they won't be executed. That way, you can test new statements without deleting the old statements.

  • To increase or decrease the indentation of several lines of code, select the lines and click the Increase Indent or Decrease Indent button. Or, press the Tab and Shift+Tab keys.

  • To move quickly between lines of code, you can use the last eight buttons on the Text Editor toolbar to set and move between bookmarks.

How to collapse or expand regions of code

  • If a region of code appears in the Code Editor with a minus sign (-) next to it, you can click the minus sign to collapse the region so just the first line is displayed.

  • If a region of code appears in the Code Editor with a plus sign (+) next to it, you can click the plus sign to expand the region so all of it is displayed.

How to use code snippets

When you add code to an application, you will often find yourself entering the same pattern of code over and over. For example, you often enter a series of if blocks like the ones in the previous figures. To make it easy to enter patterns like these, though, Visual Studio 2008 provides a feature known as code snippets. These code snippets make it easy to enter common control structures like the ones that you'll learn about in chapter 5.

Sometimes, you'll want to insert a code snippet on a blank line of text as shown in figure 3-11. In that case, you can right-click on the blank line in the Code Editor and select the Insert Snippet command from the resulting menu. When you do, a list of folders is displayed, and you can double-click the folder that contains the code snippet you want to insert. To insert a C# code snippet, for example, you can double-click the Visual C# folder. Then, you can select the shortcut name for the code snippet and press the Tab or Enter key to insert the code snippet into the Code Editor.

In this figure, for example, the code snippet named if (not the #if snippet at the top of the list) has been inserted. This snippet contains the start of an if  block. Now, you just need to enter a condition within the parentheses and some statements for the if block between the curled braces.

Other times, you'll want to surround existing lines of code with a code snippet. In that case, you can select the code that you want to surround, rightclick on that code, and select the Surround With command from the resulting menu. Then, you can select the appropriate snippet. For example, you might want to add an if block around one or more existing statements.

As I mentioned earlier in this chapter, C# 2008 also lets you insert a code snippet from a completion list. If you look back at figure 3-5, for example, you'll see that the tool tip that's displayed for the if keyword in the completion list in the first example indicates that the if statement has a code snippet. Then, you can just press the Tab key twice to insert that snippet.

If you find that you like using code snippets, you should be aware that it's possible to add or remove snippets from the default list. To do that, you can choose the Code Snippets Manager command from the Tools menu. Then, you can use the resulting dialog box to remove code snippets that you don't use or to add new code snippets. Be aware, however, that writing a new code snippet requires creating an XML file that defines the code snippet. To learn how to do that, you can consult the documentation for Visual Studio.

Incidentally, if you're new to programming and don't understand the if statements in this chapter, don't worry about that. Instead, just focus on the mechanics of using code snippets. In chapter 5, you'll learn everything you need to know about coding if statements.

The default list of Visual C# code snippets



Figure 3-9 How to use code snippets

A code snippet after it has been inserted



Figure 3-10 code snippet after it has been inserted

Description

  • To insert a code snippet, right-click in the Code Editor and select the Insert Snippet command from the resulting menu. Then, double-click the folder that contains the code snippet you want to insert (in most cases, the Visual C# folder), select the code snippet, and press the Tab or Enter key.

  • You can also insert a code snippet by selecting an item from a completion list that has a code snippet and then pressing the Tab key twice.

  • To surround existing code with a code snippet, select the code, right-click on it, and select the Surround With command from the resulting menu. Then, select the appropriate  snippet.

  • You can use the Tools Code Snippets Manager command to display a dialog box that you can use to edit the list of available code snippets and to add custom code snippets.

How to refactor code

As you work on the code for an application, you will often find that you want to revise your code. For example, you may want to change a name that you've used to identify a variable in your code to make the name more meaningful and readable. However, if you change the name in one place, you need to change it throughout your code. This is known as refactoring, and Visual Studio includes features that make it easy to refactor your code.

Figure 3-12 shows how you can use Visual Studio to quickly and easily change the names that you use within your code. In this figure, for example, the first screen shows the Code Editor after the name of the subtotal variable has been changed from subtotal to total. Here, a bar appears under the last letter of the newly renamed total variable. Then, the second screen shows the smart tag menu that can be displayed by pointing at the bar and clicking on the drop-down arrow that becomes available.

At this point, you can select the first command to rename the variable throughout the application. Or, you can select the second command to preview the changes. If you select the second command, a Preview Changes dialog box will be displayed. Then, you can preview the changes and deselect any changes that you don't want to make.

Although this figure just shows how to change a name that's used by the code, you can also use Visual Studio's refactoring features to modify the structure of your code by extracting methods, encapsulating fields, and so on. To do that, you often begin by selecting a block of code. Then, you can right-click on the code and select the appropriate refactoring command. Or, you can select the appropriate command from the Refactor menu.

If you already have experience with another object-oriented language, these refactoring features should make sense to you. If not, don't worry. You'll learn more about these features as you progress through this book.

The bar that appears under a renamed variable



The menu that's available from the bar



The Preview Changes dialog box



Figure 3-11 How to refactor code


Description

  • The process of revising and restructuring existing code is known as refactoring. Visual Studio provides many features that make it easy to refactor your code.

  • When you change a name that's used in your code, Visual Studio displays a bar beneath the  modified name. Then, you can display a smart tag menu by moving the mouse pointer over the  bar, and you can click on the drop-down list to display a menu that contains the appropriate refactoring commands.

  • You can also use Visual Studio's refactoring features to modify the structure of your code by extracting methods, encapsulating fields, and so on. To do that, you often begin by selecting a block of code. Then, you can right-click on the code and select the appropriate command,  or you can select the command from the Refactor menu.

  • Some refactoring commands display a dialog box that lets you preview the changes before you make them. Then, you can deselect any changes that you don't want to make.

Total Pages : 10 678910

comments