Step by Step: ToolboxBitmap in Visual Basic 2008 Express

 

 

Create a new project using the Class Library template and give it the name “TestClass”.

 

Save your project to your favorite directory. From Windows Explorer, locate your project directory and copy in the bitmap you want to use as your Toolbox Bitmap. Important: Rename the bitmap to the name of your class. In our case the new project created Class1.vb, so our image name must be Class1.bmp.

 


Return to Visual Basic and add references to System.Drawing and System.Windows.Forms.

 

 

From the Solution Explorer add an existing item and select the bitmap image you pasted in the project directory. You will need to change the “Objects of file type:” drop down to “All Files(*.*) “ so you can select the image.

 


Once you have added your image file the Solution Explorer should look like this.

 

Select Class1.bmp in the Solution Explorer. In the Properties window, change Build Action to Embedded Resource.

 

 


Open Class1.vb and add Imports System.Drawing and Imports System.Windows.Forms

Add the ToolboxBitmap attribute to your class. Your code should be as follows.

 

Imports System.Drawing

Imports System.Windows.Forms

 

<ToolboxBitmap("Class1.bmp")> _

Public Class Class1

    Inherits Button

 

End Class

 

 

Build your solution.

When you build you solution, your new dll will be located in .. TestClass\TestClass\bin\Release

 

Open the Toolbox in the IDE and right click in the toolbox, select “Choose Items”.

 


When presented with the Choose Toolbox Items dialog, use the Browse button to locate your newly created dll file. Once you have selected it, Class1 will show up under the .Net Frameworks Components tab. Click on Class1 in the list. In the description box at the bottom of the dialog you should see your image.

 

Extra:

You may find that you have designed your component inside of its own namespace. In this case, you must fully qualify the name of the image that you want to use as its icon.

 

Imports System.Drawing

Imports System.Windows.Forms

 

Namespace Foo

    <ToolboxBitmap("Foo.Class2.bmp")> _

    Public Class Class2

        Inherits Button

 

    End Class

End Namespace

 

You will notice in this code sample that the image name is Foo.Class2.bmp.

Because Class2 resides in namespace Foo you have to use the full name.

 

Summary:

For your ToolboxBitmap to work:

  1. The name of the image used in the attribute {<ToolboxBitmap("Foo.Class2.bmp")>} must be the name of your class. (Your class can be any component that would normally show up in the Toolbox.  Most likely you have created a component using the UserControl template. By the way, if you put your class inside a namespace, don’t forget to change your partial class so it is in the same namespace.)
  2. If your control is in a namespace other than the root namespace you must fully qualify the image.
    1. Example: Foo.Foobar.UserControl1.bmp
  3. You may be tempted to add a Windows Forms Application project to you solution for testing your control. When you do, and build your application, your controls will be available in the Toolbox. However, they will not show your toolbox icons. You can however follow the same steps for “Choose Items” and your controls will reflect the correct icons. While your editing your form, you can right click on any of the control group tabs in the toolbox and select Choose Items to add your new controls to that tab.