9.6 Programmatically Adding Controls to the Toolbox

 <  Day Day Up  >  

During the installation of your control, you want to automatically add the control's icon to the Visual Studio .NET IDE toolbox.


Technique

To add your control to the list of controls that is displayed within the Customize Toolbox without initially adding the control to the toolbox, you can create a Registry value during installation. The Registry key is

 
 HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\VisualStudio.1\AssemblyFolders\  control_name  

Create a default value for this key that is the path to the folder which contains the assembly housing your control.

To programmatically add the control icon to the toolbox, use the extensibility features of Visual Studio .NET. After obtaining the main Visual Studio .NET object, retrieve the Toolbox interface from that object, as shown in Listing 9.4. To add a new tab to the toolbox, call the Add method defined in the ToolboxTabs collection of the Toolbox interface. This step returns a ToolboxTab interface. To add the icon to that tab, call the Activate method, which selects the newly created tab, and then call the Add method defined in the ToolBoxItems collection. This method has three parameters denoting the name of the control to add, the path to the assembly that contains the control, and a value from the vsToolBoxItemFormat enumerated data type, which specifies what type of object you are adding to the toolbox. In this case, the object is a .NET control, so use the vsToolBoxItemFormatDotNETComponent value for this parameter.

Listing 9.4 Programmatically Adding an Icon to the Visual Studio .NET Toolbox
 private void btnInstall_Click(object sender, System.EventArgs e) {     if( openFileDialog1.ShowDialog(this) == DialogResult.Cancel )         return;     try     {         EnvDTE.DTE dte;         dte = (EnvDTE.DTE)System.Runtime.InteropServices.Marshal.GetActiveObject                   ("VisualStudio.DTE.7.1");         EnvDTE.ToolBox tb = (EnvDTE.ToolBox)             dte.Windows.Item(EnvDTE.Constants.vsWindowKindToolbox).Object;         EnvDTE.ToolBoxTab tbTab = tb.ToolBoxTabs.Add( "My Custom Controls" );         tbTab.Activate();         tbTab.ToolBoxItems.Add( "6_AutoCustomToolbox Control",             openFileDialog1.FileName,             EnvDTE.vsToolBoxItemFormat.vsToolBoxItemFormatDotNETComponent );     }     catch( Exception ex )     {         MessageBox.Show( ex.Message );     } } 

Comments

Deployment strategy is as important if not more important than the actual solution implementation itself. After all, if your application or control doesn't install onto a user 's system properly, then you encounter extra costs in the way of support. When implementing a control to be used by other developers, you might want to consider installing the control into the toolbox within Visual Studio .NET.

Visual Studio .NET contains substantial extensibility support, allowing you to programmatically interact and manipulate the environment using an COM-enabled language such as JavaScript, VBScript, C++, and C#. The object model, which details the hierarchical relationship of individual objects within the IDE, allows you to access almost any area imaginable related to Visual Studio. Some of these areas include supporting windows, menus , project and solution settings, and, of course, the toolbox.

To obtain a reference to the toolbox, you must first obtain a reference to the main Visual Studio .NET object. In Listing 9.4, you can see that the GetActiveObject method passes the programmatic identifier ( progid ) of the Visual Studio .NET Extensibility object. Once you do this step, you then have access to the methods , properties, and any subobjects contained within the main object. One of these objects is the ToolBox object. Because the design of Visual Studio .NET extensibility is COM- based, you'll notice that you will work with interfaces instead of .NET objects. The remainder of the code in Listing 9.4 creates a new tab within the toolbox, activates that tab so that the control icon is placed correctly, and then inserts the control.

The last parameter of the Add method is a value from the EnvDTE.vsToolBoxItemFormat enumerated data type. The value is vsToolBoxItemFormatDotNETComponent , signifying that the item being added is a .NET object. One thing to note is that you are not limited to just inserting .NET components into the toolbox. If you look at the toolbox within the IDE, you'll notice a tab named Clipboard Ring. It is a collection of toolbox items containing text that represents a history of items copied onto the Clipboard. By using the value vsToolBoxItemFormatText you can insert text, or by using vsToolBoxItemFormatHTML you can insert HTML.

 <  Day Day Up  >  


Microsoft Visual C# .Net 2003
Microsoft Visual C *. NET 2003 development skills Daquan
ISBN: 7508427505
EAN: 2147483647
Year: 2003
Pages: 440

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net