You can do this in different ways explained below. In all the cases the bitmap or icon should follow these rules:
- The bitmap or icon dimension should be 16x16 with 16 colors.
- The bottom-left pixel color will be assumed to be the transparent color.
Technique 1. Use a bitmap (not an icon) as an embedded resource without specifying the ToolboxBitmapAttribute for the type.
Assume you have a custom control MyControl in the namespace MyNamespace, create a bitmap file, MyControl.bmp, following the rules stated above. Add this file to your project at the top-level and make it an embedded resource. The project's default namespace should be MyNamespace.
If the control's namespace and the project's default namespace don't match then move the bitmap to appropriate subfolders so that they match. If this is not possible, typically when the namespaces are not related at all, then you cannot use this technique; instead use one of the techniques below using the ToolboxBitmap attribute.
Create the assembly and the next time you add it to the Toolbox the image in MyControl.bmp should be displayed in the Toolbox.
This is the easiest technique to implement as it doesn't require you to use the ToolboxBitmapAttribute in your type definition.
Technique 2. Use ToolboxBitmap attribute.
Example 1.
Use a bitmap (not an icon) in the embedded resource with the same name as the type.
Default assembly namespace: MyAssemblyNamespace
namespace MyAssemblyNamespace
{
[ ToolboxBitmap( typeof( MyCustomType ) ) ]
public class MyCustomType : Component
{ /* ... */ }
}
In the above scenario the runtime will look for an embedded BMP file with the name MyCustomType.bmp in the project's root directory. Note that the default namespace and the type's namespace match.
Example 2. Use a bitmap or icon in a subdirectory of the directory where the type is defined.
If you want your icons in subdirectories, then change the attribute like this:
[ ToolboxAttribute( typeof( MyCustomType ), "ToolboxIcons.MyCustomType.bmp" ) ]
or
[ ToolboxAttribute( typeof( MyCustomType ), "ToolboxIcons.MyCustomType.ico" ) ]
where the BMP or ICO file is in a sub-directory called ToolboxIcons.
Example 3.
Sometimes your type's namespace and the default assembly namespace may be unrelated, in which case you have to use a different type that has the same namespace as the default assembly namespace to scope the embedded image file.
Default assembly namespace: MyAssemblyNamespace
namespace MyAssemblyNamespace
{
public class SomeType
{ /* ... */ }
}
namespace DifferentNamespace
{
// Using SomeType which has the same namespace
// as the default assembly namespace to scope the embedded resource.
[ ToolboxBitmap( typeof( SomeType ), "MyCustomType.ico" ) ]
public class MyCustomType
{ /* ... */ }
}
In this case the runtime will look for the above resource at the top-most directory. If your icons were in a subdirectory named ToolboxIcons then the attribute would look like this:
[ ToolboxBitmap( typeof( SomeType ), "ToolboxIcons.MyCustomType.ico" ) ]
Contributed from George Shepherd's Windows Forms FAQ