Share via


How to: Insert Snippets Into Your Code (Visual Basic)

You can insert IntelliSense code snippets by using the navigation tools in the Code Editor or by dragging files from Windows Explorer. When you know the name of the snippet you want to insert, you can do so by typing its shortcut followed by the TAB key. When you have to browse for an appropriate snippet, you can use the snippet picker, which gives you categorized lists of snippets to choose from.

After you insert the code snippet, you modify it to work in your application. For example, you may find that a particular snippet inserts a new method in your application. If you inserted this new method inside an existing method, you would have to delete some extra lines of code to fix the compiler errors. You might also want to change any messages to the user or file names found in the snippet. For more information, see How to: Customize Snippet Code Once It Is Inserted into Your Code.

To insert a snippet by browsing in the Code Editor

  1. Right-click the Code Editor where you want to insert the code, and then click Insert Snippet. The IntelliSense code snippet picker appears.

    As an alternative to step 1, type a question mark followed by the TAB key in the Code Editor to open the code snippet picker.

  2. Navigate to the task of your choice and click it. The snippet code is inserted into your code.

    Once you have added the snippet to your code, there may be parts of it that need customization, such as replacing variable names with more appropriate names. The editor helps you with this process. For more information, see How to: Customize Snippet Code Once It Is Inserted into Your Code.

    The snippets that appear in the picker are stored as files in subfolders of specific file locations. You can examine and change the file locations by using the Code Snippets Manager. There is one file for each snippet, and the file-name extension for the files is .snippet. Snippets that you have created or acquired from another source can be stored anywhere in the file system, although they will not appear in the shortcut menu unless they reside in one of the specified folders. For more information, see snippet manager.

To insert a snippet when you know its shortcut

  • Type the shortcut for the snippet in the Code Editor followed by the TAB key. The snippet will be inserted.

    -or-

  • Type the first few characters of the shortcut name for the snippet in the Code Editor, followed by a question mark and TAB. A picker will appear with the list of snippets whose shortcuts begin with the characters you typed. As you navigate the list, the title of the selected shortcut will appear as a ToolTip.

There are two ways to find the shortcut name for a code snippet:

  • Use the Code Snippets Manager to view the shortcuts for all the snippets.

  • The shortcut is displayed as part of the description ToolTip in the code snippet picker.

To insert a snippet into your code by using Windows Explorer

  1. Open Windows Explorer.

  2. Navigate in Windows Explorer to the folder that contains the file that you want to insert. If you chose the default location for the installation of Visual Studio, the snippets are stored in C:\Program Files\Microsoft Visual Studio 9.0\VB\Snippets\1033. If you chose the default location for the installation of Visual Basic Express, the snippets are stored in C:\Program Files\Microsoft Visual Studio 9\Common7\IDE\VBExpress\Snippets\1033.

  3. Drag the file from Windows Explorer to the location in your code where you want to insert it.

See Also

Tasks

How to: Search for Code Snippets Online

Concepts

Visual Basic IntelliSense Code Snippets

Security Considerations in Using Snippets

Best Practices for Using IntelliSense Code Snippets

Reference

Creating and Using IntelliSense Code Snippets