Thursday, October 29, 2009

Hack 28. Package Your Toolbox Settings











 < Day Day Up > 





Hack 28. Package Your Toolbox Settings





If you want to be able to deploy the same

toolbox settings on a bunch of different machines, you can write a

program to add custom controls or code snippets to the toolbox
.





One of the big challenges of

team development is creating consistent code across all developers.

One way to encourage consistent code is to provide each developer

with the same set of controls and code snippets. This way, each

individual developer has all the same tools as the other developers

(whether they use them is another matter).





In Section 4.5Customize the

Toolbox" [Hack

#27]
, we covered a method

for moving toolbox settings, but using this as a method of

distribution is not a good idea. The method outlined involved copying

a user-specific file to another system. This is a great solution for

moving your own personal settings, but trying to use this method as a

means of distribution would result in the overwriting of any custom

controls or code snippets each developer may have created.





A better method for adding custom controls and code snippets to each

developer's system is to create a small program that

adds these controls and snippets through the



Visual Studio Common Environment Object

Model. In this hack, you are going to learn how to create just such a

program.





For simplicity's sake, we are going to create a

Windows Forms application that, upon the press of a button, will add

a number of custom controls to the Visual Studio toolbox. In

practice, you may find it easier to create an installation package or

command-line tool, but the code will be the same. The first thing you

need to do is create a Windows Forms Project in Visual Studio using

your favorite .NET language. (I am using C# for these examples, but a

version of VB.NET is available for download from

the book's web site, which is described in the

Preface.) Next, you will need to create a reference to the

envdte.dll



assembly�this assembly contains the objects we will work with

to modify the Visual Studio environment. This is called the Common

Environment Object Model and can be used to modify just about every

aspect of the Visual Studio IDE [Hack #86] .





After you create the obligatory using or

Imports statement for the

EnvDTE namespace, you can start to work with

the Visual Studio environment.









When working with EnvDTE, you may run across frequent

"Call was rejected by the Callee"

errors. These are due to timeouts and occur more frequently on slow

machines, but they can pop up on fast machines as well. To prevent

these errors refer to [Hack #87] .








The next thing you need to do is to get an instance of the current

DTE (see [Hack #87] ). This is not as easy as it

sounds. Because the DTE objects for Visual Studio .NET 2002 and

Visual Studio .NET 2003 are exactly the same, we have to get the

class using the progid, as shown here:





Type latestDTE = Type.GetTypeFromProgID("VisualStudio.DTE.7.1");

EnvDTE.DTE env = Activator.CreateInstance(latestDTE) as EnvDTE.DTE;











You can get a reference to the currently executing instance of Visual

Studio using this line of code:





EnvDTE.DTE dte = 

(DTE)Marshal.GetActiveObject("VisualStudio.DTE.

7.1");







This will return a reference to the currently executing instance of

Visual Studio as opposed to a reference to a new instance of Visual

Studio.








The DTE object can be used to modify

many different parts of Visual Studio. To get the toolbar window, you

need to access the Windows collection of the DTE object using the

vsWindowKindToolBox constant, then you need to cast the

object to the ToolBox type as shown in this code:





Window win = env.Windows.Item(Constants.vsWindowKindToolbox);



ToolBox toolBox = (ToolBox) win.Object;







The next thing you need to do is check to see if the



tab you want to add is already there.

If it is not already there, you will want to add it:





ToolBoxTab tab = null;



// Loop through the tab collection and see if the tab already exists

foreach (ToolBoxTab tb in toolBox.ToolBoxTabs)

{

if (tb.Name = = "Our Controls")

{

tab = tb;

}

}



// The tab does not exist so add it

if(tab = = null)

{

tab = toolBox.ToolBoxTabs.Add("Our Controls");

}







Now there is a little dirty work. The following things need to be

done because working with the DTE object can often be an adventure in

bugs. Rather than simply adding the control to the Toolbox tab, first

you need to show the property window, activate the tab, and then

select the first item. All of this is necessary to get the process to

run correctly, though admittedly does not make a lot of sense:





// Show the PropertiesWindow for bugs sake

env.ExecuteCommand("View.PropertiesWindow","");



// Activate the tab

//(Because the Add method will only add to the active tab)

tab.Activate( );



// Select the first item

//(Because this is the only way to make it work)

tab.ToolBoxItems.Item(1).Select( );







Then you need to add the toolbox item to the tab by calling the Add

method and passing in the path to the .dll that

contains your controls and the type of item you are adding. When

adding a code snippet, the first string is

the name of the snippet and the second is the value of the snippet.

When adding controls, the first string is not

used; the name of the control is used instead. Visual Studio uses the

assembly specified in the second string and looks inside it to

determine the name of the control.





In this example, I am using

System.Web.dll

,

which will add all of the controls in that assembly to the toolbox.

In practice, you will want to point to the assembly that contains

your custom controls.





// Add new toolbox items for our custom controls

ToolBoxItem tbi1 = tab.ToolBoxItems.Add("not used", _

@"C:\windows\Microsoft.NET\Framework\v1.1.4322\System.Web.dll",

vsToolBoxItemFormat.vsToolBoxItemFormatDotNETComponent);







The last thing you need to do is close the

environment:





// Close the environment

env.Quit( );







After running this code, whether in an installation procedure or a

simple Windows Form, you can open Visual Studio and you will see all

of the controls in System.Web.dll added to your

new Toolbox tab. (You may need to right-click on the toolbox and

select Show All Tabs to see the new tab.)









At the time of this writing, attempting to add code snippets to the

toolbox in this manner works for only Visual Studio .NET

2002�all attempts to get this working in Visual Studio .NET

2003 and VIsual Studio .NET 2005 have ended with only the tab being

added and no code snippets being saved. Hopefully this will be fixed

before the final release of Visual Studio 2005.








If you are shipping your own custom controls, or even using a set of

custom controls internally, this code presents a great way to install

these controls in the toolbox for all of your users. The complete

application can be downloaded in both C# and VB.NET from this

book's web site (see

http://www.oreilly.com/catalog/visualstudiohks).

















     < Day Day Up > 



    No comments:

    Post a Comment