Welcome!

.NET Authors: Liz McMillan, Mark O'Neill, Peter Silva, Yakov Werde, Matthew Pollicove

Related Topics: .NET

.NET: Article

Extending Visual Studio 2005

Build and integrate new features into Visual Studio 2005

Now we're going to make the dialog consistent with the Visual Studio UI Guidelines. If you're interested in learning more about creating a Visual Studio consistent user experience refer, to the document located in C:\Program Files\VSIP 8.0\UI Guidelines. Your Form should now resemble Figure 11. See Table 4 for the list of properties and associated values to set on your Form.

We have one step left in our UI layout. Go to the View menu and select the Tab Order menu item. Your cursor will change into a crosshair and every control will have a number superimposed on top of it. Click the controls in the following order:

  1. "Description" Label
  2. Textbox
  3. "Priority" Label
  4. Combo Box
  5. OK Button
  6. Cancel Button
Now go back to the View menu and click the Tab Order menu item again. Your dialog's tab order has been set to the proper left-to-right and top-to-bottom order. The Tab Order feature in the Windows Forms Designer makes it incredibly easy to set the tab order for all your dialogs, as seen in Figure 12.

Now, let's write some code. Add properties to the AddTaskDialog to expose a task description and priority, and wire up the properties so that they'll read and write from the appropriate controls on the form.

Now that we have the means to read and write data from our dialog, let's wire it up to the toolbar button. Reopen TasklistPackage.cs, and locate the Initialize() method. Currently, it contains the code shown in Listing 3.

What's happening is that when our Package is first loaded, we request the OleMenuCommandService from Visual Studio and add an Event Handler to the View.Other Windows.Tasklist Sample menu item.

This is a lot like when we wrote the code to add a toolbar to the tool window. We created a CommandID object that represents the CTC-defined command we want to hook. After the CommandID object is created, we construct a MenuCommand object that combines an EventHandler with the CommandID, and then pass it to Visual Studio.

Add the following code to the if statement:

// Create the command for the Add Task Button
CommandID addTaskCommandID = new CommandID(GuidList.guidTasklist_SampleCmdSet,
    (int)PkgCmdIDList.cmdidAddTask);
MenuCommand addTaskMenuCmd = new MenuCommand(new EventHandler(AddTask),
    addTaskCommandID);
mcs.AddCommand(addTaskMenuCmd);

Now add an AddTask method to your Package. This can be as simple as:

private void AddTask(object sender, EventArgs e)
{
AddTaskDialog atd = new AddTaskDialog();
    atd.ShowDialog();
}

Go ahead and run your project again. Click the Add Task button in your tool window's Toolbar and you will see your Add Task dialog appear.

For now, our data access story will be limited. Unfortunately, there's no easy way to supply a data source to a ListView control, so what we'll do for now is add an event handler to forward on the results of an AddTask operation to our ListView in the Tool Window's client area code.

First, open your Package class. We will need to add a static accessor to it so that we can easily subscribe to the event we'll create later.

private static TasklistPackage staticPackage = null;
public static TasklistPackage TasklistSingleton
{
   get
   {
    return staticPackage;
   }
}

Next, push your Package instance into the staticPackage variable in your Package's constructor:

TasklistPackage.staticPackage = this;

Next, add a delegate and event to your Package:

public delegate void TaskCreated(object sender, TaskCreatedEventArgs e);
public event TaskCreated OnTaskCreated;

Clearly we'll now need to add the new EventArgs subclass we used above, which is shown in Listing 4. Our AddTask() method needs to take advantage of the new functionality we've added. The changes we make can be seen in Listing 5.

ToolwindowClientArea needs an event handler to deal with these changes. Add the following line of code to its constructor:

TasklistPackage.TasklistSingleton.OnTaskCreated += new
TasklistPackage.TaskCreated(TaskWasCreated);

And then this method:

private void TaskWasCreated(object sender, TaskCreatedEventArgs e)
{
    AddTask(e.Description);
}

Conclusion
We've accomplished quite a bit here. We have created a tool window with a fleshed-out user interface, and a standard Visual Studio Toolbar. Now, when you start your project you should see something similar to Figure 13. Congratulations!

Next Steps
Clearly, our Task List still has a ways to go. We need to integrate ourselves into Visual Studio's Task List Service to synchronize with data in the VS Task List. We also need to disable buttons on our tool window's Toolbar when the current context doesn't allow them to function. Look for all this, and more, in the next installment of this article. To download the sample code demonstrated here, please visit http://blogs.msdn.com/aaron brethorst.

More Stories By Aaron Brethorst

Aaron Brethorst is a program manager on the Visual Studio Core team, where he is responsible for Accessibility and User Experience across the Visual Studio IDE. Aaron joined Microsoft in 2003 after graduating from the University of Minnesota with a degree in Computer Science. When he's not developing software, you can usually find him in a Seattle coffee shop with a good book.

Comments (0)

Share your thoughts on this story.

Add your comment
You must be signed in to add a comment. Sign-in | Register

In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.