Creating Code Snippets

Both Visual Basic 2005 and VSTO ship with a number of code snippets you can use in your code. You can also create your own snippets. This appendix shows you how to create a simple XML code snippet that adds a control to the actions pane.

You can use a code snippet editor tool to produce the code snippets, or you can write the code snippet XML file. To download Microsoft's Code Snippet Editor from the Microsoft Visual Basic Developer Center, search for "Code Snippet Editor for Visual Basic 2005." However, for the following example, you'll add XML tags to a file by hand.

1. Create a new text file in Notepad, and then add the XML markup from Listing A.1 to the file.

Listing A.1. An XML code snippet that adds a control to the actions pane <?xml version="1.0"?>

<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/ 2005/CodeSnippet"> <CodeSnippet Format="1.0.0"> <Header>

<Title>Add Control to Actions Pane</Title> <Author>Kathleen McGrath</Author> <Description>

Adds a control to the actions pane. </Description>

<Shortcut>addcontrol</Shortcut> </Header> <Snippet>

<Declarations> <Literal>

<ID>controlName</ID>

<Type>Object</Type>

<ToolTip>

Replace with the name of the control. </ToolTip>

<Default>DateTimePicker</Default> </Literal> </Declarations>

<Code Language="VB" Kind="method body"> <![CDATA[Me.ActionsPane.Controls.Add( New $controlName$())]]> </Code> </Snippet> </CodeSnippet> </CodeSnippets>

2. Replace the text in the <Author> tag with your name.

3. Save the file to the c:\ drive, and name it myActionsPane.snippet.

This XML file has two main tags: the <Header> tag and the <Snippet> tag. The <Header> tag contains information about the code snippet, including the title, author, description, and shortcut. The shortcut works in the same way Word's autotext feature works: It is an alias for the snippet's content. Typing the shortcut into the Code Editor and then pressing the TAB key expands the shortcut and inserts the code snippet in place of the shortcut text.

The <Snippet> tag contains variable declarations and code. In the example, the <ID> tag represents the variable name, which is of the type indicated in the <Type> tag. This creates a variable named controlName of the type Object. You can add a tool tip and a default value for the variable by using the <ToolTip> tag and <Default> tag, respectively. The <Code> tag contains the code that you want inserted into the Code Editor. The Language attribute of the <Code> tag indicates that the code example Is written In Visual Basle. This code snippet Is discoverable only in projects that are created with Visual Basic 2005.

The actual code appears between the brackets after CDATA. In the example, it is a single line of code that adds a control to the actions pane, passing the controlName variable. You can store any number of lines of code in this node. In our example, when this code is inserted into the Code Editor, controlName is highlighted and users can easily replace the default value with any other value.

You can use this simple XML example as a basis for creating other code snippets. We recommend that you also examine the preinstalled XML code snippets for Visual Basic 2005 and VSTO 2005 to learn how to create more complex code snippets.

0 0

Post a comment