Coding Skills for a Windows Form Application - How to use code snippets
(Page 5 of 5 )
When you add code to an application, you will often find yourself entering the same pattern of code over and over. For example, you often enter a series of if blocks like the ones in the previous figures. To make it easy to enter patterns like these, though, Visual Studio 2005 introduces a new feature known as
code snippets. These code snippets make it easy to enter common control structures like the ones that you’ll learn about in chapter 5.
Sometimes, you’ll want to insert a code snippet on a blank line of text as shown in figure 3-10. In that case, you can right-click on the blank line in the Code Editor and select the Code Snippet command from the resulting menu. Then, you can select the shortcut name for the code snippet you wish to insert and press the Tab key. When you do, the code snippet will be inserted into the Code Editor. In this figure, for example, the code snippet named if (not the #if snippet at the top of the list) has been inserted into the document. This snippet contains the start of an if block. Now, you just need to enter a condition within the parentheses and some statements for the if block between the curled braces.
Other times, you’ll want to surround existing lines of code with a code snippet. In that case, you can select the code that you want to surround, right-click on that code, and select the Surround With command from the resulting menu. Then, you can select the appropriate snippet. For example, you might want to add an if block around one or more existing statements.
If you find that you like using code snippets, you should be aware that it’s possible to add or remove snippets from the default list. To do that, you can choose the Code Snippets Manager command from the Tools menu. Then, you can use the resulting dialog box to remove code snippets that you don’t use or to add new code snippets. Be aware, however, that writing a new code snippet requires creating an XML file that defines the code snippet. To learn how to do that, you can consult the documentation for Visual Studio.
Incidentally, if you’re new to programming and don’t understand the if statements in this chapter, don’t worry about that. Instead, just focus on the mechanics of using code snippets. In chapter 5, you’ll learn everything you need to know about coding if statements.
The default list of code snippets

Figure 3-10. How to use code snippets
A code snippet after it has been inserted

Description
- To insert a code snippet, right-click in the Code Editor and select the Code Snippet command from the resulting menu. Then, select the code snippet you wish to insert.
- To surround existing code with a code snippet, select the code, right-click on it, and select the Surround With command from the resulting menu. Then, select the appropriate snippet.
- You can use the Tools->Code Snippets Manager command to display a dialog box that you can use to edit the list of available code snippets and to add custom code snippets.
Please check back next week for the conclusion of this article.
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |
|
This article is excerpted from chapter three of the book Murach's C# 2005, written by Joel Murach (Murach, 2005; ISBN: 9781890774370). Check it out today at your favorite bookstore. Buy this book now.
|
|