Writing VS .NET Macros

 < Day Day Up > 

After you get the IDE set up the way you like it, take a look at the VS .NET macros. Many developers never discover (or never use) this facet of VS .NET, which is a shame. One of the things that separates the beginning coder from the seasoned developer is an attitude toward repetitive work. This applies not just to writing code but to performing repetitive actions in the IDE as well. Perhaps you need to apply a comment with a copyright notice to the top of every code module. You could do this using cut and paste, but there’s an alternative: VS .NET lets you record and replay macros, collections of actions that can simulate almost anything that you can do with the mouse and keyboard.

Overview of Macros

If you’ve used macros in Microsoft Word or Excel, or worked with Windows Scripting, you should have a pretty good mental model of macros. A macro, in this case, is a series of actions saved in a scripting language. The group of actions can be stored and later replayed. The goal of macros is to simplify repetitive work by letting you do just one thing (run the macro) instead of an entire series of things (the actions contained in the macro).

VS .NET includes everything you’d expect from a modern high-end macro language:

  • A macro recorder and playback facility

  • A macro editor

  • Keyboard bindings for macros

  • An object model to let macros work with the IDE

Let’s briefly look at each of these facets of VS .NET macros.

start sidebar
Alternatives to VS .NET

Although I’m focused on VS .NET in this chapter (and throughout the book), you should be aware that you have alternatives for developing .NET applications. Depending on your budget, other tools, and needs, one of these tools might be right for you.

  • Notepad You don’t actually need an IDE at all to build .NET software. The .NET Framework SDK (www.microsoft.com/downloads/details.aspx?FamilyId=9B3A2CA6-36474-070-9F41-A333C6B9181D&displaylang=en) includes command-line compilers and tools that work just as well as VS .NET for developing .NET applications. You can use Notepad (or any other text editor, such as SciTE, www.scintilla.org/SciTE.html&e) to build your source code. I wouldn’t recommend this approach, though, unless you have a favorite text editor that you simply can’t bear to part with.

  • SharpDevelop SharpDevelop (sometimes called #develop) is a free, open-source IDE for both C# and VB .NET projects. SharpDevelop offers an excellent environment for begin-ners who don’t want to invest in VS .NET. You can download a copy from www.icsharpcode.net/OpenSource/SD/.

  • Antechninus C# Editor The Antechinus C# Editor (www.c-point.com/csharp.htm) is a $35 program that provides a good deal of help if you’re working in C#. Its features include IntelliSense, links to the .NET documentation, code-completion templates, a C# tutorial, and syntax coloring, just to name a few.

  • ASP.NET Web Matrix ASP.NET Web Matrix (www.asp.net/webmatrix/default.aspx?tabIndex=4&tabId=46) is another free IDE, this one developed by the ASP.NET team and community. It’s very small (1.3-MB download), but even so it includes some features that aren’t present in VS .NET, such as a built-in web server and design-time rendering of user controls. It’s definitely worth a look if you’re doing mostly ASP.NET development.

  • C#Builder C#Builder is Borland’s entry into the .NET IDE world. The C#Builder home page is at www.borland.com/csharpbuilder/. The product comes in a variety of editions, including a free personal edition (which, however, is pretty feature poor). C#Builder is worth testing if you’re using Borland’s other software lifecycle tools or if .NET/ Java 2 Enterprise Edition (J2EE) interoperation is high on your list.

  • PrimalCode PrimalCode, from Sapien Technologies (www.sapien.com/), is an IDE aimed strictly at the developer who prefers to work in source code; there are no visual designers at all. Its advantages are a small footprint, support for multiple web technologies (including Active Server Pages, PHP, and Java Server Pages in addition to ASP.NET), and a nice set of integrated tools. It’s an interesting alternative if your primary needs are in ASP.NET and web services.

  • Eclipse Eclipse (www.eclipse.org/) is a free, open, extensible IDE that has strong support in the Java world and an impressive array of features. Although Eclipse isn’t a .NET IDE in its default configuration, Improve Technologies (www.improve-technologies.com/alpha/esharp/) distributes a free C# plug-in that allows you to edit C# files in Eclipse. If you work mostly in Java, this may be the easiest way for you to dabble in C#.

  • Snippet Compiler Although it’s not a full IDE, Jeff Key’s excellent Snippet Compiler is worth mentioning. Snippet Compiler (shown below) nestles down on your Windows Taskbar until you need it, when it presents you with a simple way to test code. This is great when you’re trying to figure out how to do something and don’t want the overhead of firing up VS .NET. You can get a free copy from www.sliver.com/dotnet/SnippetCompiler/.

    click to expand

  • IntelliJ IDEA IDEA (www.intellij.com/idea) is a well-respected Java IDE. As of this writing, the company doesn’t offer a .NET version, but it has announced that it’s working on one (no delivery date has been announced, however). If it manages to bring the full suite of refactoring tools over, it could well be a force to be reckoned with.

end sidebar

The Macro Recorder

VS .NET makes it extremely easy to get started writing macros. That’s because it includes a macro recorder that lets you create a macro simply by carrying out the actions that the macro should contain. After you’ve launched VS .NET and loaded your project, follow these steps to record a macro:

  1. Select Tools Macros Record Temporary Macro or press Ctrl+Shift+R to start the Macro Recorder. This will open the Recorder toolbar, shown in Figure 6.4.

    Figure 6.4: The Recorder toolbar lets you start, pause, finish, or cancel macro recording.

  2. Carry out your actions just as you normally would. To create a macro to add copyright notices, I first pressed Ctrl+Home to move to the top of the window, typed the copyright notice, and pressed Enter to insert a blank line.

  3. Click the Stop Recording button on the macro toolbar.

To prove that the new macro works, you can test it out. Open a new code module in the editor, and then select Tools Macros Run Temporary Macro. VS .NET will add the copyright notice to the new module.

The Macro Editor

Recording macros is simple, but sometimes recorded macros will need a bit of cleanup to be really useful. The tool for doing this cleanup is the Macro Editor. To see your new macro in the Macro Editor, follow these steps:

  1. Select View Other Windows Macro Explorer. This will open the Macro Explorer window, which shows all of the macros that you have loaded. Macros are organized into projects, each of which can contain many modules. In turn, each module can contain many macros. You’ll find your new macro in a module named RecordingModule in a project named MyMacros.

  2. Right-click on the TemporaryMacro macro and select Edit. This will open the macro in the Macro IDE, shown in Figure 6.5. Alternatively, you can press Alt+F11 to open the Macro IDE.

click to expand
Figure 6.5: The Macro IDE

Here (slightly reformatted) is the code that the Macro Recorder produced for the copyright macro:

 Sub TemporaryMacro()  DTE.ActiveDocument.Selection.StartOfDocument  DTE.ActiveDocument.Selection.NewLine  DTE.ActiveDocument.Selection.LineUp  DTE.ActiveDocument.Selection.Text = _   "// Copyright (c) 2003-2004 Lark Group, Inc. " & _   "See license.txt for licensing details."  DTE.ActiveDocument.Selection.NewLine  End Sub 

As you can see, VS .NET macros are written in VB .NET (even if they’re recorded in a C# project). The DTE (design-time environment) object is the root of an object model that encompasses the entire VS .NET IDE. This object model is far too complex to explain in detail here, but you’ll find it documented in the VS .NET help files (look for the “Common Environment Object Model” section of the documentation).

Making a Macro Permanent

Of course, a macro is most useful if it’s available whenever you need it. The temporary macro will be overwritten whenever you record a new macro. To keep the macro, you’ll want to save it in a macro project of its own (or in a macro project with other macros). Follow these steps to move the macro to a permanent project:

  1. Right-click on the root node in the Macro Explorer, and select New Macro Project. This will open the New Macro Project dialog box. Give your new project a name and save it in a convenient spot.

  2. The previous step creates a new macro project containing a default Module1. Double-click on Module1 to open it in the Macro IDE.

  3. Paste the macro into the module. This is a good time to give it a sensible name, such as AddCopyrightComment.

  4. Click the Save button on the Macro IDE toolbar to save the changes.

Your new macro project will now be loaded every time you launch VS .NET. If you want to stop this automatic loading, you can right-click the macro project in the Macro Explorer and select Unload Macro Project.

Macros from the Keyboard

Now that the macro is permanently loaded, you can make it easy to run by binding it to a keystroke combination. VS .NET lets you attach any macro to any keystroke through its Options dialog box. Follow these steps to bind the macro to a key combination:

  1. Select Tools Options

  2. In the Options dialog box, open the Environment folder and select the Keyboard node.

  3. Scroll down the list of commands until you find the macro that you just saved. It will be located at Macros.MacroProjectName.ModuleName.MacroName.

  4. Place the cursor in the Press Shortcut Keys box and press the key combination that you want to assign to the macro—for example, Ctrl+Shift+3. VS .NET will warn you if the key combination is already in use.

  5. Click OK to make the assignment.

Now you can add the copyright comment to a new module with the press of a few keys.


There’s a lot more to know about macros, especially the details of the DTE object model (which is also used by add-ins, discussed in the next section of this chapter). An excellent reference is Inside Microsoft Visual Studio .NET (Microsoft Press, 2002), by Brian Johnson, Craig Skibo, and Marc Young.

 < Day Day Up > 

Coder to Developer. Tools and Strategies for Delivering Your Software
Coder to Developer: Tools and Strategies for Delivering Your Software
ISBN: 078214327X
EAN: 2147483647
Year: 2003
Pages: 118

Similar book on Amazon

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