Smart Coding: Using IntelliSense to Help You Write Code
The Visual Basic integrated development environment (IDE) helps you write code with fewer keystrokes and fewer errors by providing lists of the available keywords, variables, and members (methods, properties, and events). The IDE also completes words as you type your code. In Visual Basic, you can get all the help you need right in the Code Editor, while you type your code.
There are several features of IntelliSense that can make your coding tasks easier. These include List Members, Parameter Info, Quick Info, Complete Word, and Syntax Tips, and also some new features introduced in Visual Basic 2008.
When you type the name of a type or namespace in the Code Editor, a list of all the valid methods, properties, and events becomes available in a drop-down list. An example of code written within a method that displays the list members is shown in the following illustration.
You can scroll through the list or type the first few letters of the member to move quickly to that member in the list. Then press the ENTER key to add that member to your code. For more information, see List Members.
When a method takes parameters, Visual Basic displays information about the parameters, such as the type of parameter, the name, and the number of parameters required. If a function is overloaded, you will see UP and DOWN arrows that enable you to scroll through all the function overloads, as shown in the following illustration.
As you type the parameter, the list displays the next parameter in bold font. For more information, see Parameter Info.
You can display the complete declaration of an identifier in your code by holding your mouse pointer over the identifier. The following illustration shows the Quick Info box that appears. For more information, see Quick Info.
IntelliSense can complete words for you as you type. When you have typed enough letters in a word to disambiguate it from other words, you can complete the word by pressing ALT+RIGHT ARROW or CTRL+SPACEBAR. For example, you can type "Me.Button1.Te" to narrow down the words in the list, then press ALT+RIGHT ARROW to complete the word. For more information, see Complete Word.
The following illustration shows an example of the completion list that appears when you type code in the Code Editor.
New in Visual Basic 2008
There are several new features that have been added to IntelliSense, which is often referred to as "IntelliSense Everywhere". True to its name, IntelliSense Everywhere provides IntelliSense lists for more than just members. There's Keyword IntelliSense, Expression Lists, Syntax Tips, and IntelliSense for local variables.
Keyword IntelliSense enables you to see a list of keywords that are available, depending on the context. At the file-level, IntelliSense is available for keywords such as Imports and Option. You will also see a list of declaration-level keywords that filter based on the syntax, such as the For and While keywords.
Expression Lists appear when you type various expressions. For example, when you assign a value to a variable, an Expression List will appear after you type the equal sign.
Syntax Tips provide information about the syntax of a statement. For example, when you type the keyword Select, IntelliSense displays Select Case <testExpression>, which indicates the syntax to use.
Local variables appear in a list at the start of a line in a method, when you type arguments for a method call, and in any expression context for which they are in scope.
These IntelliSense improvements make it easier for you to write your code without having to look up syntax in Help or in the object browser. Another improvement is to the IntelliSense lists themselves.
Transparent Lists. Sometimes you might find that code in the IDE that you have to reference is covered by an IntelliSense list. You can press the CTRL key to temporarily make the list transparent so that you can see the code underneath it. When you release the CTRL key, the list returns.
List Filtering. Instead of displaying the whole list, IntelliSense displays only the words that begin with the letters you have typed. For example, if you want to create a variable for a PrintDialog and you type Dim printDocument As New Print, the list will display only those members that begin with the text "Print". This makes it quicker and easier to find the item you're looking for in the list. If you do want to see the complete list, you can press CTRL+J. When you start typing again, the list will again become filtered.
To use IntelliSense
On the File menu, click New Project.
The New Project dialog box appears.
Click Windows Forms Application and then click OK.
Drag a Button control from the Toolbox to the form.
Double-click the button to enter the default Click event hander in the Code Editor.
Type Me followed by a period.
A list of members appears.
Press the CTRL key and notice that the list becomes transparent. Then release the CTRL key.
Type the letter V and notice that the list is filtered to show only those members that begin with "V".
Press CTRL+J to display the complete list again.
Type the letter i and then press CTRL+SPACEBAR to insert the word Visible in your code.
Finish the code by typing = True, and notice the IntelliSense that appears when you type the equal sign and when you type the word "True."
In the next lesson, you will learn how to use the keyboard to navigate the IDE.
Next Lesson: Keyboard Shortcuts: Keyboard Shortcuts: Navigating the IDE by Using the Keyboard.