Intelli Sense Code Snippets

One of the nice features of Visual Studio 2005 is the ability to use IntelliSense code snippets. These are XML files that enable you to insert a shell procedure into your code and then "fill in the blanks" to make the code snippet specific to your application. They are called IntelliSense code snippets because they are available as you type code directly in the Visual Studio Code Editor, just as other IntelliSense features are available when you type a period after an object name.

You can access IntelliSense code snippets by typing the shortcut name of the code snippet and pressing the TAB key, or by right-clicking the Code Editor and selecting Insert Snippet. Visual Studio ships with a number of IntelliSense code snippets for VSTO, and they are organized by language and category. If you have created a VSTO project for Visual Basic, then when you use the Insert Snippet feature, only the code snippets written in Visual Basic are visible. When you select Insert Snippet from the shortcut menu, a number of folders become visible, as shown in Figure 2.27.

¡Insert Snippet:


□ Data - Designer features and ADO.NET


□ Data Types - defined by Visual Basic

m File system - Processing Drives, Folders, and Files

□ Math

Office Development

E2 Security

□ Smart Devices

rn Windows Forms Applications

□ Windows Operating System


Figure 2.27. IntelliSense code snippets folders for Visual Basic

Figure 2.27. IntelliSense code snippets folders for Visual Basic

You can then navigate through the folders to the desired snippet. Figure 2.28 shows the code snippets available in the Excel Controls folder under Office Development.

In addition to preinstalled IntelliSense code snippets for VSTO, Visual Studio ships with a number of code snippets for common coding patterns.

Insert Snippet: Office Development > Excel > Controls >

i=J Add a Smart Tag to an Excel Workbook

ia) Bind Data to a List Object

i=l Bind Data to a NamedRange Control

Filter a List Object


jhlides or shows an Excel worksheet. |

i=J Hide Worksheet Controls

i=J Populate Array from Range

i=J Populate Range from Array

l=J Show Totals Row of List Object

Figure 2.28. IntelliSense code snippets in the Office Development | Excel | Controls folder

Figure 2.28. IntelliSense code snippets in the Office Development | Excel | Controls folder

These snippets include conditionals and loops (such as a Do While loop), exception handling (such as Try Catch statements), and properties, procedures, and type declarations. If a code snippet has an assigned shortcut, you can type the shortcut name and press the TAB key to insert the code snippet into the Code Editor. The code snippet in Figure 2.29 appears in the Code Editor after you type Property followed by pressing the TAB key.

Private neivrPropertyValue

As Integer

Public Property NewProperty() As Integer

Return newPropertyValue

End Get

Set(ByVal value As Integer)

newPropertyValue = value End Set End Property

Figure 2.29. IntelliSense code snippet creating a property

Visual Studio creates placeholders and highlights each of the items that can be replaced in the code snippet. You can tab from placeholder to placeholder. If you hold your cursor over a placeholder, Visual Studio displays a tool tip that gives you additional information about how to replace the contents of the placeholder. You can learn how to create your own custom XML code snippets in Appendix A.

0 0

Post a comment