Walkthrough: Adding a Command to an Editor Generated by the Package Wizard

You can use the Visual Studio Integration Package Wizard to generate an embedded editor that inherits from the RichTextBox class. You can use this custom editor either as a starting point for other editor applications or as a model for a similar embedded editor. This walkthrough shows how to add a command to the editor that the wizard generates.

Note

Beginning with Visual Studio 2008 SDK, use XML Command Table (.vsct) files instead of command table configuration (.ctc) files to define how menus and commands appear in your VSPackages. For more information, see Visual Studio Command Table (.Vsct) Files.

Prerequisites

To run and build this walkthrough, you must have Visual Studio 2008 and the Visual Studio SDK installed.

Creating a VSPackage for a Custom Editor

To create an editor VSPackage

  1. Create a VSPackage named TestEditor. For more information, see How to: Create VSPackages (C# and Visual Basic).

  2. In the Visual Studio Integration Package Wizard, set the programming language to Visual C# and select Custom Editor, and then set the editor name to Test Editor, the file name extension to testext, and default file name to TestExtFile.

Adding the Command Code to the Generated Editor

The Visual Studio Integration Package Wizard creates a sample embedded editor that adds commands from the standard VSConstants.VSStd97CmdID command set. In this section, you add the required code to create your own command.

To add the command code

  1. In Solution Explorer, open PkgCmdID.cs and then declare your command after the existing declarations in the body of the PkgCmdIDList class.

  2. Close PkgCmdID.cs and then open TestEditor.vsct.

  3. In the <Symbols> section, find the <GuidSymbol> node whose name attribute ends in "CmdSet." Declare your command as an <IDSymbol> element in this node.

  4. The Visual Studio Integration Package Wizard generates the symbol for a menu group named "MyMenuGroup." Create an entry for MyMenuGroup in the <Groups> section and set its parent Guid:Id pair to guidSHLMainMenu:IDM_VS_MENU_EDIT so that it is puton the Edit menu.

  5. In the <Buttons> section, create an entry for the cmdidColorFont command that you declared in step 3 of this procedure.

    The parent settings put the command in the menu group that you defined in step 4. The visibility flags guarantee that the Color Text command is displayed on the Edit menu only when it is activated in code.

  6. Close TestEditor.vsct and then open EditorPane.cs

  7. In the #region Command Handling Functions, add the following line to the setupCommands() method, after the existing addCommand statements, to associate the cmdidColorFont command with its event handlers:

    By binding them in this manner, the onQueryColorFont method will be called when the user opens the Edit menu, and the onColorFont method will be called when the user clicks the Color Text command.

  8. After the setupCommands() method, add the onQueryColorFont and onColorFont event handlers.

    The onQueryColorFont method makes the Color Text menu command visible only when text is selected and that the selected text is not already crimson. The onColorFont method changes the color of the selected text.

  9. Build the solution and check for errors.

To test your editor command

  1. Press F5 to start an instance of the Visual Studio experimental build. 

  2. On the File menu of Visual Studio Exp, point to New and then click File. In the Categories pane of the New File dialog box, click Test Editor.

  3. In the Templates pane, double-click Test Editor.

    Doing this opens a text file in the custom editor.

  4. Open the Edit menu.

    Notice that only Select All is enabled.

  5. Type text in the editor pane and then select some of it.

    Notice that the commands, Copy, Paste, Cut, and Color Text are enabled.

  6. Click Color Text to color the selected text crimson.

Next Steps

To add a keyboard shortcut to the ColorText command, see How to: Bind Keyboard Shortcuts to Menu Items

See Also

Other Resources

Editor Walkthroughs