Share via


Walkthrough: Exposing Properties to the Properties Window (C#)

This walkthrough exposes the public properties of an object to the Properties window. The changes you make to these properties are reflected in the Properties window.

Exposing Properties to the Properties Window

In this section, you create a simple tool window package and display the public properties of the associated window pane object in the Properties window.

To expose properties to the Properties window

  1. Create a new Visual Studio Integration Package project named MyObjectProps.

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

    The Package Wizard starts.

  2. In the Select a Programming Language page, select Visual C#.

  3. In the Select VSPackage Options page, select Tool Window.

  4. In the Tool Window Options page, change the Window Name to "My Object Properties", and then click Finish.

    The wizard creates the managed project, MyObjectProps.

  5. Open the file, MyToolWindow.cs, and add the following fields to the MyToolWindow class:

  6. add the following code to the MyToolWindow class:

    private ITrackSelection TrackSelection
    {
        get
        {
            if (trackSel == null)
                trackSel =
                   GetService(typeof(STrackSelection)) as ITrackSelection;
            return trackSel;
        }
    }
    
    public void UpdateSelection()
    {
        ITrackSelection track = TrackSelection;
        if (track != null)
            track.OnSelectChange((ISelectionContainer)selContainer);
    }
    
    public void SelectList(ArrayList list)
    {
        selContainer = new SelectionContainer(true, false);
        selContainer.SelectableObjects = list;
        selContainer.SelectedObjects = list;
        UpdateSelection();
    }
    
    public override void OnToolWindowCreated()
    {
        ArrayList listObjects = new ArrayList();
        listObjects.Add(this);
        SelectList(listObjects);
    }
    

    The TrackSelection property uses GetService to obtain an STrackSelection service, which provides an ITrackSelection interface. The OnToolWindowCreated event handler and SelectList method together create a list of selected objects that contains only the tool window pane object itself. The UpdateSelection method tells the Properties window to display the public properties of the tool window pane.

  7. Build and start the project in debug mode by pressing the keyboard shortcut, F5. This starts Visual Studio Exp.

    Note

    Both versions of Visual Studio are open now.

  8. If the Properties window is not visible, open it by pressing F4.

  9. In Visual Studio Exp, on the View menu, point to Other Windows, and then click the My Object Properties window.

    The window opens and the public properties of the window pane appear in the Properties window.

  10. Click in Solution Explorer. The properties in the Properties window disappear. Click the My Object Properties window. The properties reappear.

  11. Change the Caption property in the Properties window to Something Else.

    The My Object Properties window caption changes accordingly.

Exposing Tool Window Properties

In this section, you add a tool window and expose its properties. The changes you make to properties are reflected in the Properties window.

To expose tool window properties

  1. Exit Visual Studio Exp.

  2. Open the file, MyToolWindow.cs, and add the public boolean property IsChecked to the MyToolWindow class:

    ObjectProperties#3
    
  3. In the MyToolWindow constructor, add a this keyword to the MyControl construction:

    control = new MyControl(this);
    
  4. Open the file, MyControl.cs, in Code view. Replace the constructor with the following code:

    private MyToolWindow pane;
    public MyControl(MyToolWindow pane)
    {
        InitializeComponent();
        this.pane = pane;
        checkBox1.Checked = pane.IsChecked;
    }
    

    This gives MyControl access to the MyToolWindow object.

  5. Change to Design view.

  6. Delete the button control. Add a CheckBox from the Toolbox to the upper, left corner.

  7. Double-click the CheckBox.

    This creates the checkBox1_CheckedChanged event handler and opens it in the code editor.

  8. Replace the body of the checkbox event handler with the following code:

    private void checkBox1_CheckedChanged(object sender, System.EventArgs e)
    {
        pane.IsChecked = checkBox1.Checked;
        pane.UpdateSelection();
    }
    
  9. Replace the body of the MyControl constructor with the following code:

    InitializeComponent();
    this.pane = pane;
    checkBox1.Checked = pane.IsChecked;
    
  10. Open the file MyControl.Designer.cs and make checkBox1 public:

    public System.Windows.Forms.CheckBox checkBox1;
    
  11. Build and start the project in debug mode by pressing the keyboard shortcut, F5. This starts Visual Studio Exp.

  12. In Visual Studio Exp, on the View menu, point to Other Windows, and then click My Object Properties.

    The public properties of the window pane appear in the Properties window. The IsChecked property appears under the category, My Properties.

  13. Click the IsChecked property.

    The description MyControl Properties appears at the bottom of the Properties window.

  14. Click the checkbox in the My Object Properties window. IsChecked changes to True. Click it again to clear it. IsChecked changes to False. Change the value of IsChecked in the Properties window. The checkbox in the My Object Properties window changes to match the new value.

    Note

    If you must dispose of a property or object displayed in the Properties window, call OnSelectChange with a null selection container first. After disposing the property or object, you can change to a selection container with updated SelectableObjects and SelectedObjects lists.

Changing Selection Lists

In this section, you add a selection list for a simple property class and use the tool window interface to choose which selection list to display.

To change selection lists

  1. Exit Visual Studio Exp.

  2. Open the file, MyToolWindow.cs, and add the public class, Simple, to the beginning of the file, just after the namespace statement:

    public class Simple 
    {
        private string someText = "";
    
        [Category("My Properties")]
        [Description("Simple Properties")]
        [DisplayName("MyText")]
        public string SomeText 
        {
            get { return someText; }
            set { someText = value; }
        }
    
        [Category("My Properties")]
        [Description("Read-only property")]
        public string ReadOnly
        {
            get { return "Hello"; }
        }
    }
    

    An object of type Simple has the public string properties, SomeText, and ReadOnly.

  3. Add this code to the end of the MyToolWindow class, just after the IsChecked property:

    private Simple simpleObject = null;
    public Simple SimpleObject
    {
        get
        {
            if (simpleObject == null) simpleObject = new Simple();
            return simpleObject;
        }
    }
    public void SelectSimpleList()
    {
        ArrayList listObjects = new ArrayList();
        listObjects.Add(SimpleObject);
        SelectList(listObjects);
    }
    
    public void SelectThisList()
    {
        ArrayList listObjects = new ArrayList();
        listObjects.Add(this);
        SelectList(listObjects);
    }
    

    This creates the singleton property, SimpleObject, and two methods to switch the Properties window selection between the window pane and the Simple object.

  4. Open the MyControl.cs file in Code view. Add these lines of code to the end of the checkBox1_CheckedChanged event handler, after the call to the UpdateSelection method:

    if (pane.IsChecked)
        pane.SelectSimpleList();
    else pane.SelectThisList();
    
  5. Build and start the project in debug mode by pressing the keyboard shortcut, F5.

    This starts Visual Studio Exp.

  6. In Visual Studio Exp, click the View menu, point to Other Windows, and then click My Object Properties.

    The window opens and the public properties of the window pane appear in the Properties window.

  7. Click the check box in the My Object Properties window. The Properties window displays the Simple object properties, SomeText and ReadOnly. Click the check box again to clear it. The public properties of the window pane appear in the Properties window.

    Note

    The display name of Some Text is My Text.

Best Practice

In this walkthrough, ISelectionContainer is implemented so that the selectable object collection and the selected object collection are the same collection. Only the selected object appears in the Property Browser dropdown list. For a more complete ISelectionContainer implementation, see the Reference.ToolWindow samples in the SDK.

See Also

Concepts

Support for the Property Browser

Other Resources

VSPackage State