Welcome to the third part of a five-part article series on how to code and test a Windows Forms application. In this article you will learn a number of useful ways to deal with code, such as how to correct syntax errors, how to use code snippets, and so forth. This article is excerpted from chapter three of Murach's Visual Basic 2008, written by Anne Boehm (Murach, 2008; ISBN: 1890774456).
As you enter code, Visual Studio checks the syntax of each statement. If a syntax error, or build error, is detected, Visual Studio displays a wavy line under the code in the Code Editor. In the Code Editor in figure 3-9, for example, you can see wavy lines under three different portions of code. Then, if you place the mouse pointer over one of the errors, Visual Basic will display a description of the error.
If the Error List window is open as shown in this figure, any errors that Visual Studio detects are also displayed in that window. Then, you can double-click on an error message to jump to the related code in the Code Editor. After you correct a coding problem, its message is removed from the Error List window.
If the Error List window isn’t open, you can display it by selecting the Error List command from the View menu. When you’re learning Visual Basic, you’re going to make a lot of coding errors, so it makes sense to keep this window open. But after you get used to Visual Basic, you can conserve screen space by using the Auto Hide button so this window is only displayed when you point to the Error List tab.
By the way, Visual Studio isn’t able to detect all syntax errors as you enter code. So some syntax errors aren’t detected until the project is built. You’ll learn more about building projects later in this chapter.
The Code Editor and Error List windows with syntax errors displayed
Description
Visual Studio checks the syntax of your Visual Basic code as you enter it. If a syntax error (or build error) is detected, it’s highlighted with a wavy underline in the Code Editor, and you can place the mouse pointer over it to display a description of the error.
If the Error List window is open, all of the build errors are listed in that window. Then, you can double-click on any error in the list to take you to its location in the Code Editor. When you correct the error, it’s removed from the error list.
If the Error List window isn’t open, you can display it by selecting the Error List command from the View menu.
Visual Studio doesn’t detect some syntax errors until the project is built. As a result, you may encounter more syntax errors when you build and run the project.
Figure 3-9.How to detect and correct syntax errors
The topics that follow present some other skills for working with code. You’ll use many of these skills as you code and test your applications.
How to use the toolbar buttons
Whenever you work with a Windows application like Visual Studio, it’s worth taking a few minutes to see what’s available from the toolbar buttons. When you’re entering or editing code, both the Standard and Text Editor toolbars provide some useful functions, and they are summarized in figure 3-10.
During testing, you can comment out several lines of code by selecting the lines of code and clicking the Comment Out button in the Standard toolbar. Then, you can test the application without those lines of code. Later, if you decide you want to use them after all, you can select the lines and click the Uncomment button to restore them. Similarly, you can use the Increase Indent and Decrease Indent buttons in the Text Editor toolbar 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 button to set bookmarks on specific lines of code, you can move between the marked lines by clicking the next and previous buttons. Although you usually don’t need bookmarks when you’re working with simple applications, bookmarks can be helpful when you’re working with large applications.
As you get more Visual Basic experience, you should also experiment with the first three buttons on the Text Editor toolbar. You’ll find that they provide quick access to information like the member list for an object or the parameter list for a function.
How to collapse or expand code
As you write the code for an application, you may want to collapse or expand some of the code. To do that, you can use the techniques described in figure 3-10. When you collapse the procedures that are already tested, it’s easier to find what you’re looking for in the rest of the code. And if you want to print the source code for a class, you don’t have to print the collapsed code.
How to print the source code
Sometimes, it helps to print the code for the class that you’re working on in the Code Editor window. To do that, you use the Print command in the File menu. Then, if you don’t want to print the collapsed code, you check the Hide Collapsed Regions box. When the code is printed, any lines that extend beyond the width of the printed page are automatically wrapped to the next line.
The Code Editor and the Text Editor toolbar
How to use the Standard toolbar to comment or uncomment lines
Select the lines and click the Comment Out or Uncomment button. When you comment out coding lines during testing, you can test new statements without deleting the old ones.
How to use 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 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.
Figure 3-10. How to use the toolbars and collapse or expand code
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, Visual Studio provides a feature known as code snippets. These snippets make it easy to enter common control structures like the ones that you’ll learn about in chapter 5.
To insert a code snippet on a blank line of text as shown in figure 3-11, right-click on the blank line in the Code Editor and select the Insert Snippet command from the shortcut menu. Then, double-click the name of the group (like Code Patterns), double-click the name of the subgroup (like Conditionals and Loops), and double-click the name of the snippet you want to insert.
At that point, the code snippet is inserted into the Code Editor. In this figure, for example, you can see that If, ElseIf, Else, and End If lines have been inserted into the code. Now, you just need to replace the words True and False with conditions and enter the Visual Basic statements that you want executed for the If, ElseIf, and Else clauses.
Although code snippets make it easy to enter common patterns of code, it can be cumbersome to access them using the shortcut menu. Because of that, you may want to use the shortcuts for the code snippets you use most often. In the second screen in this figure, for example, the tool tip for the selected code snippet indicates that the shortcut for that code snippet is IfElseIf. To insert this code snippet using its shortcut, you just enter the shortcut and press the Tab key.
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. 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 list that’s displayed for the Code Patterns group
The default list of code snippets for the Conditionals and Loops group
The If…ElseIf…Else…End If 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, select the code snippet you wish to insert. You can also insert a snippet by entering its shortcut and pressing the Tab key.
Before you can select a snippet, you must get to the group that it’s in. For instance, to use the snippet shown above, you must select the Code Patterns group followed by the Conditionals and Loops group.
Once a snippet has been inserted into your code, you can replace the highlighted portions with your own code and add any other required code. To move from one highlighted portion of code to the next, you can press the Tab key.
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.
As you work on the code for an application, you may decide to change the name of a variable, procedure, class, or other identifier. When you do that, you’ll want to be sure that you change all occurrences of the identifier. Figure 3-12 shows you two techniques you can use to rename an identifier.
The first technique shown in this figure is to rename the identifier from its declaration. Here, the name of the discountPercent variable is being changed to discountPct. When you change an identifier like this, a bar appears under the last character of the name as shown in the first screen. Then, you can move the mouse pointer over the bar and click the drop-down arrow that appears to display a smart tag menu. This menu shows a Rename command that you can use to change all occurrences of the identifier in your project.
You can also rename an identifier from any occurrence of that identifier. To do that, just right-click on the identifier, select the Rename command, and enter the new name into the Rename dialog box that’s displayed.
How to rename an identifier from its declaration
The bar that appears under a renamed identifier
The menu that’s available from the bar
How to rename an identifier from any occurrence
Description
Visual Studio lets you rename identifiers in your code, such as variable, procedure, and class names. This works better than using search-and-replace because when you use rename, Visual Studio is aware of how the identifier is used in your project.
When you change the declaration for an identifier in your code, Visual Studio displays a bar beneath the last character of the identifier. Then, you can move the mouse pointer over the bar, click the drop-down arrow that’s displayed, and select the Rename command from the smart tag menu.
You can also rename an identifier from anywhere it’s used in your project. To do that, right-click the identifier and select the Rename command from the shortcut menu to display the Rename dialog box. Then, enter the new name for the identifier.
Figure 3-12.How to rename identifiers
Please check back tomorrow for the continuation of this article.