Walkthrough: Putting a User Control in a Tool Window

This walkthrough demonstrates the process of adding a user control to a tool window implemented by a VSPackage.

A user control is a collection of Windows controls bound together in a single control. Adding a user control to a tool window is straightforward and all that is really needed is to obtain the user control to appear in the Toolbox. The user control that is used in this walkthrough is the clock control developed in Walkthrough: Authoring a Composite Control with Visual C#.

Prerequisites

This walkthrough requires the Visual Studio SDK to be installed. The result of this walkthrough writes information to the experimental registry hive for Visual Studio 2008.

Creating the User Control Project

To create the ctlClockLib solution

  1. Follow the procedure that is described in Walkthrough: Authoring a Composite Control with Visual C# to create the clock control. Do not create the alarm clock control.

    The following procedures assume that only the clock control is created, not the alarm clock control.

  2. Close the ctlClockLib solution.

  3. Continue to "Creating a Tool Window VSPackage".

    注意

    The following procedures assume that you have named the clock control ctlClock as described in the walkthrough that was mentioned earlier.

Creating a Tool Window VSPackage

To create the MyToolWindowUC VSPackage

  1. On the File menu, point to New and select Project.

  2. In the New Project dialog box, select Other Project Types and then select Extensibility from the Project types list.

  3. In the Templates list, select Visual Studio Integration Package.

  4. Type the name of your project; for example, MyToolWindowPackageUC (the rest of this walkthrough assumes that name). Select a destination for the project.

  5. Click OK to open the Visual Studio Integration Package Wizard.

  6. On the Select a Programming Language page, select Visual C# and accept the default for the key file. Click Next.

  7. On the Select VSPackage Options page, select Tool Window. Click Next.

  8. On the Tool Window Options page, set the Window Name to MyToolWindowUC and set the Command ID to cmdidMyToolWindowUC. Click Finish.

  9. After several moments, the Package Wizard finishes building the project. You obtain the "Creating project 'MyToolWindowPackageUC'…project creation successful" message on the status bar. Continue to add a user control to your project.

Adding the User Control

To add the ctlClockLib User Control

  1. In Solution Explorer, right-click Solution 'MyToolWindowPackageUC', point to Add and select Existing Project.

  2. In the Add Existing Project dialog box, open the ctlClockLib project and select the ctlClock.lib.csproj project file. Click OK. This enables the user control that you can use in the designer.

  3. In Solution Explorer, right-click MyControl.cs and select View Designer. This opens the forms designer that shows the generated control for the tool window.

  4. Open the Toolbox, find the section labeled ctlClockLib Components and double-click the ctlClock control in that section to add the control to the form. As soon as you hide the Toolbox, you should see a time display in your tool window's form.

  5. In the designer, select the clock control that was just added and change the Anchor property to just be Top.

  6. In the Solution Explorer, right-click the ctlClockLib project node and select Properties.

  7. Select the Signing tab and check Sign the assembly. In the Choose a strong name key file text box, select <browse> and navigate to the key.snk file in the MyToolWindowPackageUC project folder.

  8. Compile the program and make sure there are no errors. This step registers the VSPackage and its tool window with Visual Studio 2008.

  9. Open an instance of the experimental Visual Studio 2008 with one of the following steps:

    • At the command prompt, type devenv /rootsuffix exp

    • On the Start menu, select the experimental Visual Studio 2008 shortcut; for example, Visual Studio 2008 Experimental. This shortcut is added when the Visual Studio SDK is installed.

  10. In the experimental Visual Studio 2008, open the View menu and select Other Windows and then select MyToolWindowUC. This displays your tool window with a running time display.

See Also

Concepts

Walkthrough: Authoring a Composite Control with Visual C#