Visual Studio Hacks [Electronic resources]

Andrew Lockhart

نسخه متنی -صفحه : 172/ 54
نمايش فراداده

Hack 27. Customize the Toolbox

Everything you need can be at your fingertips in Visual Studio's toolbox. In this hack, learn how to enhance the toolbox with custom controls and code snippets.

The toolbox is used to store controls and code snippets. The controls and code snippets can be dragged from the toolbox to either the editor or the code window. The toolbox is designed to save time during development. Instead of writing control code manually, you can drag and drop controls or code snippets.

4.5.1. Controls and the Toolbox

The most common use of the toolbox is for Windows controls and web controls (including HTML controls). When you work with either the Windows Forms designer or the Web Forms Designer, you will constantly interact with the toolbox to drag and drop controls onto the designer. By default, the toolbox contains controls from within the .NET Framework, but you can also add custom controls to the toolbox. Adding new controls to the toolbox is a relatively simple procedure.

First, right-click on the tab of the toolbox you want to add the control to and select Add/Remove Items; this will display the dialog shown in Figure 4-12.

Figure 4-12. Customize Toolbox dialog

From this dialog, you simply need to select your control from the list of .NET Framework Components or COM Components or use the Browse button to manually browse for the control. When browsing for controls, you need to select the assembly that the control is in, either the assembly that you created for your custom controls or the assembly provided by the control author.

One of the Windows Forms controls not added to the toolbox by default is the PropertyGrid control. Follow these steps to add this control to your toolbox:

Right-click on the Toolbox tab to which you want to add the control and select Add/Remove Items.

Scroll through the .NET Framework Components list and find the control labeled PropertyGrid.

Check the box next to the control.

Click the OK button.

You will then see the PropertyGrid control added to your tab. You can see the PropertyGrid control added to the toolbox in Figure 4-13.

Figure 4-13. PropertyGrid control in toolbox

When a control does not include a custom icon, you will see a small gear next to the control in the toolbox, as seen in Figure 4-14. This is the default icon used when the control author does not provide a custom icon.

If you are building your own control, it's easy to add a custom toolbox icon: create a 16 16 bitmap and add it to your project. You will need to ensure that the name of the bitmap is the same as the name of the control and the bitmap is set to be an embedded resource.

Figure 4-14. New control in toolbox

4.5.2. Code Snippets and the Toolbox

The normal use of the toolbox includes adding and removing tabs and controls, but you can also use the toolbox to store code snippets. Whether you want to store a method call to get a connection string, a commonly used piece of JavaScript, or an architecture-specific piece of code, the toolbox is a great place to save those pieces of code that you constantly find yourself typing.

Before adding any code snippets to the toolbox, it is a good idea to add a separate tab to the toolbox for your code snippets: right-click on the toolbox and select Add Tab, then enter a name for the tab (such as Code Snippets) in the text box that appears at the bottom of the toolbox.

Storing a piece of code in the toolbox is simple: select the code from the document window and drag it over to the toolbox. After dropping the code on the toolbox, you will see a snippet like the one shown in Figure 4-15.

Figure 4-15. Toolbox code snippet

This code snippet can now be dragged and dropped from the toolbox to the document window, and the included code will be pasted into the document. You can also rename the snippet by right-clicking on the item, selecting Rename Item, and then specifying the new name. Changing the name of the item will not affect the text that is copied to the document window.

Using the toolbox as a method to store code snippets is an effective way to reduce coding time and increase your own consistency.

4.5.3. Hacking the Hack

Now that you have customized the toolbox, it is important to be able to move those settings between systems. To move toolbox settings from one machine to another, you need to copy a single file from your profile on the current machine to your profile on the target machine. The file is called toolbox.tbd and is located in the following directory:

\Documents and Settings\<username>\Local Settings\Application Data\Microsoft\VisualStudio\<7.1>\

This file can be copied to the target system, and all of your toolbox settings will be moved to the new system when you restart Visual Studio. Keep in mind that for custom controls, any assemblies that the toolbox references would need to be copied as well.