Using the System.Drawing.ToolboxBitmap attribute in VB.NET

  • Thread starter Thread starter Nathan Sokalski
  • Start date Start date
N

Nathan Sokalski

I am attempting to create icons for controls I have created using VB.NET by
using the System.Drawing.ToolboxBitmap attribute. I have managed to do this
in C# by specifying the path to the *.ico file, but I have been unable to
get any of the overloads to work in VB.NET. I would like to store the *.ico
files in a *.resx file so that users do not need anything other than the
*.dll, but at the moment I am just trying to get any of the overloads of
System.Drawing.ToolboxBitmap to work in VB.NET. Here is my code and
directories (obviously not all files are listed, but I think these are the
ones of significance):

NateCtrl2005 (this is the name of my project)
My Project
Resources.resx (this is the resources file with my *.ico files)
ToolboxIcons (folder where I am storing the *.ico files)
ConditionalRequiredTextValidator.ico (Build Action=Embedded
Resource)
ConditionalRequiredTextValidator.vb


Code in ConditionalRequiredTextValidator.vb:

Namespace NathanSokalski
<System.Drawing.ToolboxBitmap("C:\Inetpub\wwwroot\NateCtrl2005\ToolboxIcons\ConditionalRequiredTextValidator.ico")>
Public Class ConditionalRequiredTextValidator
 
in Nathan Sokalski wrote :
I am attempting to create icons for controls I have created using
VB.NET by using the System.Drawing.ToolboxBitmap attribute.

In VB.NET, I just add a bmp file to the project and select «Embedded
Resource» in the «Build Action» property.

Then I use the third new (Type, String) of the TooboxBitmapAttribute.
 
MSDN http://msdn.microsoft.com/en-us/library/4wk1wc0a(VS.80).aspx

The following I picked up on the web but do not remember where

Setting Toolbox Bitmaps
By default, icons that you make display a little gear in the control
toolbox. It's a cute icon, but it would- n't be very informative if you have
dozens of controls that all display the same icon, particularly if you don't
use the toolbox's list view so the icons and tooltips are the only tools you
have for finding the controls you want.
Unfortunately, setting a control's toolbox bitmap is rather tricky. To set
the bitmap, you add a Toolbox Bitmap attribute to the control's class. This
attribute has a couple of different constructors. The simplest gives the
complete path to the control's 16_16-pixel bitmap file. The following code
shows this type of attribute for the StyledListBox control:
<ToolboxBitmap("C:\StyledListBox\Resources\tbxStyledListBox.bmp")> _
Public Class StyledListBox
...
This version of the ToolboxBitmap attribute is simple and reliable.
Unfortunately, it ties the code to a specific location. If you later move
the toolbox bitmap, the statement no longer works.
To avoid this problem, you can just leave the control and its files in the
location where you initially build them. Another solution is to place all of
your toolbox bitmaps in a directory somewhere and never move them. These
solutions work, but restrict what you can do with the control's project. For
example, if you email the project to someone else, they must modify the
attribute's constructor so it can find the bitmap on the new computer.
Another constructor provided by the ToolboxBitmap attribute takes as
parameters the type of a class contained in an assembly, and then the name
of a bitmap resource in that assembly. The following code shows this version
of the attribute for the StyledListBox control:
<ToolboxBitmap(GetType(StyledListBox), "tbxStyledListBox")> _
Public Class StyledListBox
...
This code tells Visual Basic to look in the assembly containing the
StyledListBox class for the resource named tbxStyledListBox.
In theory, this is a nice solution that lets the attribute find the bitmap
resource even if you move the project. In practice, getting this to work can
be tricky.
First, add a 16_16-pixel bitmap resource to the project and draw the image
you want to display in the toolbox. Next, select the bitmap file in the
Solution Explorer. In the Properties window, set the bitmap's Build Action
property to Embedded Resource.
If all goes well, the control will get the correct toolbox bitmap.
Unfortunately, if there's any kind of problem (for example, if Visual Basic
cannot find the file or the class), you won't see an error message and the
control gets the default gear icon.
If you change the control's icon and still see the wrong icon in the
toolbox, click the control in the tool- box, press the Delete key, and
confirm that you want to remove the control from the toolbox. Next, right-
click the toolbox and select Choose Items. Click the Browse button and find
the compiled control's DLL or EXE file. When you select the file and click
Open, the dialog adds the controls contained in that file to its list and
selects them. Click the control's entry to preview its toolbox bitmap. If
you see the gear bitmap (which you may be getting sick of by this point),
click Cancel so you don't add the control to the toolbox (so you don't have
to remove it again) and try again.
 
Back
Top