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.

List Members

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.

List Members

List Members

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.

Parameter Info

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.

Parameter Info

Parameter Info with Overloads Image

As you type the parameter, the list displays the next parameter in bold font. For more information, see Parameter Info.

Quick 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.

Quick Info

Quick Info Image

Complete Word

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.

Complete Word


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

  1. On the File menu, click New Project.

    The New Project dialog box appears.

  2. Click Windows Forms Application and then click OK.

  3. Drag a Button control from the Toolbox to the form.

  4. Double-click the button to enter the default Click event hander in the Code Editor.

  5. Type Me followed by a period.

    A list of members appears.

  6. Press the CTRL key and notice that the list becomes transparent. Then release the CTRL key.

  7. Type the letter V and notice that the list is filtered to show only those members that begin with "V".

  8. Press CTRL+J to display the complete list again.

  9. Type the letter i and then press CTRL+SPACEBAR to insert the word Visible in your code.

  10. 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.