VSIP: Exposing Properties and Stuff

This walkthrough guides you through the steps of exposing the public properties of any object to the Property Browser. Tools windows and document windows expose properties this way. The changes you make to properties through these windows can be reflected in the Property Browser.

· Exposing Properties to the Property Browser

· Exposing Tool Window Properties

· Changing Selection Lists

Exposing Properties to the Property Browser

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

To expose properties to the Property Browser

1. Create a new Visual Studio Integration Package project named, for example, MyObjectProps. For more information on creating a managed VSPackage, see the How to: Create a VSPackage Using the Visual Studio Integration Package Wizard topic in the VSIP 2005 beta 2 documentation.

The Package Wizard runs.

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". Click Finish.

The wizard creates the managed project MyObjectProps and the unmanaged resource-only project MyObjectPropsUI.

5. Open the file MyObjectProps/My Object Properties.cs and add this code to the end of the MyToolWindow class, just after the Dispose method:

base.Dispose( disposing );

}

private ITrackSelection trackSel;

private ITrackSelection TrackSelection {

get {

if (trackSel == null)

trackSel =

GetService(typeof(STrackSelection)) as ITrackSelection;

return trackSel;

}

}

private SelectionContainer selContainer;

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 containing only the tool window pane object itself. The UpdateSelection method tells the Property Browser to display the public properties of the tool window pane.

6. Build and launch the project in debug mode by pressing the keyboard shortcut, F5. This launches Visual Studio Exp.

Note Both versions of Visual Studio are open at this time.

7. From Visual Studio Exp, select the View/Other Windows menu item. Select the My Object Properties window.The window opens and the public properties of the window pane appear in the Property Browser.

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

9. Change the Caption property in the Property Browser 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 Property Browser.

To expose tool window properties

1. Close Visual Studio Exp.

2. Open the file MyObjectProps/My Object Properties.cs and add the public boolean property IsChecked to the end of the MyToolWindow class, just after the OnToolWindowCreated method

SelectList(listObjects);

}

private bool isChecked = false;

[Category("My Properties")]

[Description("MyControl properties")]

public bool IsChecked {

get { return isChecked; }

set {

isChecked = value;

control.checkBox1.Checked = value;

}

}

3. In the MyToolWindow constructor, add a this pointer 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;

}

This gives MyControl access to the MyToolWindow object.

5. Change to design view.

6. Select the button and remove the anchors from the Anchor property. Shrink the button to a reasonable size in the lower left corner of the window. Drag 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 checkbox event handler with the following code:

private void checkBox1_CheckedChanged(object sender, EventArgs e)

{

pane.IsChecked = checkBox1.Checked;

pane.UpdateSelection();

}

9. Make checkBox1 public:

public CheckBox checkBox1;

10. Add this line to the MyControl constructor:

InitializeComponent();

this.pane = pane;

checkBox1.Checked = pane.IsChecked;

11. Build and launch the project in debug mode by pressing the keyboard shortcut, F5. This launches Visual Studio Exp.

12. From Visual Studio Exp, select the View/Other Windows menu item. Select the My Object Properties window.The window opens and the public properties of the window pane appear in the Property Browser. The IsChecked property appears under the category My Properties.

13. Select the IsChecked property. The description MyControl Properties appears.

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

Note If you need to dispose of a property or object displayed in the Property Browser, 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. Close Visual Studio Exp.

2. Open the file MyObjectProps/My Object Properties.cs and add the public class Simple to the beginning of the file, just after the namespace statement:

namespace Vsip.MyObjectProps2

{

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 single public string property SomeText.

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

set { isChecked = value; }

}

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 Property Browser selection between the window pane and the Simple object.

27. Open the MyControl.cs file in code view. Add these lines of code to the end of the checkbox event handler:

private void checkBox1_CheckedChanged(object sender, EventArgs e)

{

pane.IsChecked = checkBox1.Checked;

if (pane.IsChecked)

pane.SelectSimpleList();

else pane.SelectThisList();

}

4. Build and launch the project in debug mode by pressing the keyboard shortcut, F5. This launches Visual Studio Exp.

5. From Visual Studio Exp, select the View/Other Windows menu item. Select the My Object Properties window.The window opens and the public properties of the window pane appear in the Property Browser.

6. Click on the checkbox in the My Object Properties window. The Property Browser displays the Simple object properties SomeText and ReadOnly, and nothing else. Click the checkbox again to uncheck it. The window pane properties reappear.

Note The display name of Some Text is My Text.

Enjoy, Martin Tracy, Programmer Writer

Important note: This material is provided in an 'as is' condition so that you might evaluate it for your own use at your own risk. You agree that by providing comments, suggestions, or other feedback related to our products, technology or services to the individuals listed above, you allow Microsoft, at its option, to use your feedback in our products, technology, and services without any obligation to you. Due to the volume of e-mails we receive, Microsoft, including the individual listed above, may not be able respond to your e-mail.