One Step Further: Inserting Code Snippets


One Step Further: Inserting Code Snippets

If you enjoyed using the system clock and other Windows resources in this chapter, you might enjoy one additional example that uses the Computer.Info namespace to display useful information about the operating system you're currently using. This example also demonstrates an interesting new feature of Visual Studio 2005 called the Insert Snippet command, which lets you insert ready-made code templates or snippets into the Code Editor from a list of common programming tasks. Visual Studio 2005 comes automatically configured with a library of useful code snippets, and you can add additional snippets from your own programs or from online resources such as MSDN. The following exercise shows you how to use this useful feature.

Insert the Current Windows Version Snippet

  1. On the File menu, click the New Project command, and create a new project named My Windows Version Snippet.

    The new project is created, and a blank form appears in the Designer.

  2. Create a new button object in the middle of the form, and set the Text property of the button to “Display Windows Version”.

  3. Double-click the button object to display the Button1_Click event procedure.

    Now you'll use the Insert Snippet command to insert a code template that automatically returns information about the version of Windows installed on your computer. Note that this particular snippet is just one example from a list of dozens of useful code templates.

  4. Click the Edit menu, point to the IntelliSense submenu, and then click the Insert Snippet command.

    The Insert Snippet list box appears in the Code Editor, as shown in the following illustration:

    graphic

    The Insert Snippet list box is a navigation tool that you can use to explore the snippet library and insert snippets into your program at the insertion point. To open a folder in the list box, double click the folder name. To return to the previous folder in the folder hierarchy, press the Backspace key.

  5. Scroll to the bottom of the list box, and then double click the Windows Operating System folder.

    In this folder you'll find snippets related to querying and setting operating system settings.

  6. Double click the System Information folder.

    A list of system information snippets appears. Now you'll select the snippet that returns information about the current version of Windows.

  7. Double click the snippet entitled “Determine the Current Windows Version.”

    Visual Studio inserts the following two lines of code into the Button1_Click event procedure at the insertion point:

    Dim osVersion As String  osVersion = My.Computer.Info.OSVersion

    These statements declare the string variable osVersion to hold version information about the operating system, and then use the Computer.Info namespace to fill the variable with current information. The snippet also uses the My object to gather information about your computer. The My object is a new “speed-dial” feature of Visual Studio 2005 designed to reduce the time it takes to code common tasks, and I will introduce it more fully in Chapter 13, “Exploring Text Files and String Processing.”

    This code snippet is called a template because it supplies the majority of the code that you need to insert for a particular task, but the code is not fully integrated into your project yet. In this case, we should add a second variable to hold the name of the operating system (because there are different Windows versions), and we'll add a MsgBox function to display the results for the user. (In other cases, you might need to add controls to your form, create new variables or data structures, or write additional program statements that use the snippet.)

  8. Press the Enter key twice to add a blank line below the snippet.

  9. Type the following program statements:

    Dim osName As String osName = My.Computer.Info.OSFullName MsgBox(osName & vbCr & osVersion)

    These statements declare a second variable named osName that will hold the Windows version retrieved by the OSFullName method of the Computer.Info namespace. There is also a MsgBox function that displays the two returned values: the operating system name (osName) and the operating system version number (osVersion). As you probably know, the operating system version number has now become quite detailed in Microsoft Windows, because Windows has the ability to be updated automatically over the Web each time a new security update or improvement is released. Examining the version number is therefore a handy way to see whether your system is up-to-date and safe. Your screen looks like this:

    graphic

  10. Click Save All to save your changes, and specify the c:\vb05sbs\chap07 folder as the location.

  11. Click Start Debugging to run the program.

    Visual Studio runs the program in the IDE.

  12. Click the Display Windows Version button to display the version information returned by the snippet.

    Your dialog box looks similar to the following:

    graphic

  13. Click OK to close the dialog box, and then click the Close button to end the program.

You've learned a handy skill that will allow you to insert a variety of useful code templates into your own programs.

TIP
To insert new snippets or reorganize the snippets you have, click the Code Snippets Manager command on the Tools menu. The Code Snippets Manager dialog box gives you complete control over the contents of the Insert Snippet list box, and also contains a mechanism for gathering new snippets online.



Microsoft Visual Basic 2005 Step by Step
Microsoft Visual Basic 2005 Step by Step (Step by Step (Microsoft))
ISBN: B003E7EV06
EAN: N/A
Year: 2003
Pages: 168

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net