WindowsForms

How to add icon for your custom UserControl to the Toolbox

March 19, 2010 .NET, HowTo, WindowsForms No comments

I read about ToolboxBitmapAttribute and while doing my usual work have decided to use it.

First of all I created my UserControl named BufferedControl which should provide double buffering functionality for me. Once I finished with my control I navigated to Toolbox to add new tool like on the picture:

And do you know what happend next? It said to me that there is no controls which could be added to the Toolbox. But why? It is derived from UserControl and looks fine. So in order to see what is the difference I added empty UserControl1 and was able to add it to the Toolbox. I thought that the reason is because I do not have designer file for my control and some specific methods like Initialize(), but after I added them nothing helped. I did more research and finally figured out that I did not have default public constructor. My constructor was like here:

        public
BufferedControl(IBufferedControlController controller)
        {
            Controller = controller;
            BufferContext = new BufferedGraphicsContext();
            SizeGraphicsBuffer();
           
SetStyle(ControlStyles.OptimizedDoubleBuffer, false);
            SetStyle(ControlStyles.DoubleBuffer,
false);
        }

So lets move to adding icon of your UserControl to the Toolbox

1) First add your control and draw the icon. Below is screenshot of what I have for this.

a) Once you done, you should ensure that your control has public default constructor and that OnPaint method will not raise exceptions after it was called on instance created with default constructor.
b) Also go to properties of your icon and set Build Action –> Embedded Resource.

2) Add ToolboxBitmap attribute to your control class declaration. In my case it looks like below:

namespace
Som.Application.Grid
{
    [ToolboxBitmap(“Som.Application.Grid.BufferedControl.ico”)]
    public class BufferedControl : UserControl
    {
      //…

3) Navigate to ToolBox and add your control from your assembly:

That’s it!


No comments


Button in pressed/unpressed state in System.Windows.Forms

January 26, 2010 QuickTip, UI, WindowsForms No comments

Do you want to see your button to be in pressed or unpressed state when you hit it once?

Did you catch that this behavior is not really button’s behavior – it is more checkBox’s behavior.

So to accomplish your goal you need move checkBox to your form and set appearance from “Normal” to “Button”. Like here:

System.Windows.Forms.CheckBox checkBox1 = new System.Windows.Forms.CheckBox();
checkBox1.Appearance = System.Windows.Forms.Appearance.Button;

Kick me if you don’t want to see such posts on my blog. :)


No comments