Reputation: 4954
I'm creating a custom toolbox in windows forms (like the one you pick the controls in the VS Form Designer). To get the image that appears on side of the text of the control I'm using this:
ToolboxItem tbi = item as ToolboxItem;
var image = tbi.Bitmap;
Where item can be any type of Control. It's working as it should be.
The problem happens when I try to use custom controls in the toolbox. Let's say I create this class:
public class PMLButton : Button
{
}
Then when the PMLButton is passed on the 'item' variable it get the gear icon, of a custom control, as it should. But in this case I would like to show the default button icon for this item.
All tools I'll use in the toolbox are derived from a default windows control class (Button, Label, Checkbox, etc).
If I cast the 'item' variable to its base type, it would work correctly and show the right image. Is there any way of doing this?
Upvotes: 1
Views: 116
Reputation: 26436
The ToolBoxItem
uses reflection to get the type, so casting it won't change its behavior.
The easiest solution is assigning the toolbox bitmap yourself:
[ToolboxBitmap(typeof(Button))]
public class PMLButton : Button
{
}
Alternatively if the control containing the bitmap is always one step up the inheritance tree, you could use:
var image = new ToolboxItem(item.GetType().BaseType).Bitmap;
I would definitely go for the attribute solution though, as PMLButton
would have a button bitmap in the designer, too.
Upvotes: 2