Attaching an Icon for the Toolbox


By default, the icon that appears in the Toolbox next to your control’s name is a gear-shaped icon. However, you can use an attribute on the class declaration that defines your control to specify a different icon to place in the Toolbox. The attribute needed is the ToolboxBitmap attribute. It can be used in several ways.

If the icon you want to use is already defined for another control, then you can have your control get the icon out of the existing control. Suppose, for example, you want to use the icon for a Textbox as the icon for your TrafficLight control. In that case, the line that declares the class

 Public Class TrafficLight

should be changed to add the attribute as follows:

  <ToolboxBitmap(GetType(System.Windows.Forms.TextBox))> _ Public Class TrafficLight 

You can also use an icon that resides in a graphic file. Several of these are included with Visual Studio in a zip file installed in the Common7\VS2005ImageLibrary subdirectory of the VS.NET directory (which is usually under Program Files on your main system drive). Or, you can define your own icons in the Paint accessory of Windows by defining an image size of 16 × 16 pixels and then painting the icon.

In either case, you’ll want your icon to be part of the assembly containing your control. Add your icon to the project that contains your control, and set its Build Action property to Embedded Resource. Then the ToolboxBitmap attribute is used to refer to the icon in the assembly, as shown here:

  <ToolboxBitmap(GetType(TrafficLight), "MyIcon.bmp")> _ Public Class TrafficLight 

This overload of the ToolboxBitmap attribute first takes a Type object, which indicates the location of the assembly containing the type. The second argument specifies the resource within that assembly to use.

After adding the attribute, just rebuild the control to incorporate the icon in the control’s DLL. Note that you must remove the control from the Toolbox and re-add it to see the changed icon. Also note that the icon won’t show up for the automatic Toolbox entry for the control at the top of the Toolbox - it is only used when the control is placed in the Toolbox via the Choose Items option.




Professional VB 2005 with. NET 3. 0
Professional VB 2005 with .NET 3.0 (Programmer to Programmer)
ISBN: 0470124709
EAN: 2147483647
Year: 2004
Pages: 267

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