Share via


Walkthrough: Retrieving Project Properties

This walkthrough guides you through the steps of creating a managed VSPackage that displays project properties in a tool window. You can create a sample managed VSPackage using the Visual Studio Integration Package Wizard. Use automation with the resulting VSPackage to display project properties in a tool window. Carry out the following steps to create a managed VSPackage with a tool window and then extend it with automation:

  • Create the managed VSPackage.

  • Call the automation model to display project properties in a tool window.

To create a managed VSPackage

  1. Create a new Visual Studio VSPackage project named ProjectProps using the Visual Studio Integration Package wizard. Go to step 2 in this procedure when you get to the Select a Programming Language page.

    For more information on creating a managed VSPackage, see How to: Create VSPackages (C# and Visual Basic).

  2. In the Select a Programming Language page, set the language to Visual C#.

  3. Leave the default values as-is in the Basic VSPackage Information page.

  4. In the Select VSPackage Options page, select the check boxes next to the Menu Command and Tool Window options.

  5. In the Command Options page, enter My Toolbox.

  6. In the Tool Window Options page, enter ProjectProps Tool Window.

  7. Click the Finish button.

    The wizard generates a managed project ProjectProps.

  8. Build the solution and verify that it compiles without errors.

To call the automation model to display project properties in a tool window

  1. In the Visual Studio Solution Explorer window, right-click the ProjectProps project node and select Add Reference.

  2. In the .NET tab view of the Add Reference dialog box, double-click EnvDTE and then click OK.

    This establishes a reference to the EnvDTE namespace.

  3. Add the following line at the top of the VSPkg.cs file:

    Imports EnvDTE
    
    using EnvDTE;
    
  4. Add the following line at the top of the body of the ProjectProps class:

    Public Shared dte As EnvDTE.DTE
    
    public static EnvDTE.DTE dte;
    
  5. Add the following line at the top of the body of the ShowToolWindow method:

    dte = CType(GetService(GetType(DTE)), DTE)
    
    dte = (DTE)GetService(typeof(DTE));
    

    This code uses GetService method to get a DTE automation object that represents the Visual Studio environment.

  6. Open MyControl.cs with the Visual Studio designer and remove the Click Me! button inserted by the Visual Studio Integration Package Wizard.

  7. Open MyControl.cs with the source code editor. Add the following line at the top of the body of the MyControl class:

    Private treeView1 As TreeView
    
    private TreeView treeView1;
    
  8. Replace the body of the MyControl constructor with the following lines:

    Public Sub New()
        ' This call is required by the Windows.Forms Form Designer.
        InitializeComponent()
    
        ' 
        ' treeView1
        ' 
        Me.treeView1 = New System.Windows.Forms.TreeView()
        Me.SuspendLayout()
        Me.treeView1.Location = New System.Drawing.Point(20, 20)
        Dim project As EnvDTE.Project
        project = ProjectProps.dte.Solution.Projects.Item(1)
        Dim nodeNum As Integer = 0
        Me.treeView1.Name = "ProjectPropsTree"
        Me.treeView1.Nodes.Add(New System.Windows.Forms.TreeNode(project.Name & " Properties"))
        For Each [property] As EnvDTE.Property In project.Properties
            Dim node As System.Windows.Forms.TreeNode = New System.Windows.Forms.TreeNode("Node" & nodeNum)
            node.Name = "Node" & nodeNum
            nodeNum = nodeNum + 1
            node.Text = [property].Name
            treeView1.Nodes(0).Nodes.Add(node)
        Next [property]
        Me.treeView1.Size = New System.Drawing.Size(250, 500)
        Me.treeView1.TabIndex = 1
        Me.Controls.Add(Me.treeView1)
        Me.ResumeLayout(False)
    End Sub
    
    public MyControl()
    {
        // This call is required by the Windows.Forms Form Designer.
        InitializeComponent();
    
        // 
        // treeView1
        // 
        this.treeView1 = new System.Windows.Forms.TreeView();
        this.SuspendLayout();
        this.treeView1.Location = new System.Drawing.Point(20, 20);
        EnvDTE.Project project;
        project = ProjectProps.dte.Solution.Projects.Item(1);
        int nodeNum = 0;
        this.treeView1.Name = "ProjectPropsTree";
        this.treeView1.Nodes.Add(new System.Windows.Forms.TreeNode(project.Name + 
                                                                   " Properties"));
        foreach (EnvDTE.Property property in project.Properties)
        {
            System.Windows.Forms.TreeNode node = 
                           new System.Windows.Forms.TreeNode("Node" + nodeNum);
            node.Name = "Node" + nodeNum++;
            node.Text = property.Name;
            treeView1.Nodes[0].Nodes.Add(node);
        }
        this.treeView1.Size = new System.Drawing.Size(250, 500);
        this.treeView1.TabIndex = 1;
        this.Controls.Add(this.treeView1);
        this.ResumeLayout(false);
    }
    

    This code uses DTE automation objects to retrieve the project properties and to dynamically populate the tree control inside the tool window with the project properties name values.

  9. Build the ProjectProps project.

  10. Run the ProjectProps project by pressing the keyboard shortcut, F5 or CTRL+F5 to launch the Visual Studio experimental build.

    Note

    Both versions of Visual Studio are open at this time.

  11. In Visual Studio Exp, create or open any project.

  12. On the View menu, point to Other Windows and select ProjectProps Tool Window.

    You should see the tree control inside the tool window with the project properties name values.

See Also

Tasks

How to: Create and Control Tool Windows

Concepts

Visual Studio SDK and Automation

Automation Model