I want to make a square with a 0 in it and when I click on it would change into a 1. (e.g. For use in a memory register in which I can set the bits manually.)
Piece of cake I thought, derive from the Checkbox class and override the OnPaint method. It was indeed easy, here is my code.
Just made an empty WindowsApp and added this:

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        private BinarycheckBox BinCB;
            
        public Form1()
        {
            InitializeComponent();

            //initialize new binary checkbox
            BinCB = new BinarycheckBox();
           
            BinCB.AutoSize = true;
            BinCB.Location = new System.Drawing.Point(70, 95);
            BinCB.Name = "bin";
            BinCB.Size = new System.Drawing.Size(25, 17);
            BinCB.TabIndex = 0;
            BinCB.Text = "bla bla";
            BinCB.UseVisualStyleBackColor = true;
            this.Controls.Add(BinCB);          
        }
    }
}

This is the code for the derived CheckBox class :

namespace WindowsFormsApplication1
{
    class BinarycheckBox : CheckBox
    {
        //init vars for OnPaint
        private Font font = new Font("Ariel", 8);
        private static Brush bluebrush = new SolidBrush(Color.Blue);
        private Brush backgroundbrush = new SolidBrush(Color.Yellow);
        Pen bluepen = new Pen(bluebrush);  
        private StringFormat strFormat = new StringFormat();

        //default constructor
        public BinarycheckBox(){}

        //we do our own painting here
        protected override void OnPaint(PaintEventArgs pevent)
        {
            base.OnPaint(pevent);   //needed
            Graphics G = pevent.Graphics;
            strFormat.Alignment = StringAlignment.Center;
            strFormat.LineAlignment = StringAlignment.Center;

            RectangleF R = new RectangleF(G.ClipBounds.X, G.ClipBounds.Y, 16F, 16F);

            G.FillRectangle(backgroundbrush, R);
            if (this.Checked)         
                //G.DrawString("1", font, bluebrush, R.X + 2, R.Y+ 1); //has the same effect
                G.DrawString("1", font, bluebrush, R, strFormat);
            else
                G.DrawString("0", font, bluebrush, R, strFormat);
        
            G.DrawRectangle(bluepen, R.X, R.Y, R.Width - 1, R.Height - 1);          
        }
    }
}

Now the problem : When I click on the box (which contains 0 at startup) it changes as expected to 1. When I click again on it the number is shifted down a few pixels and an ugly line appears above it, when I release the mouse the normal thing happens and 1 becomes 0. I'd like to know why and I also like to know if this is a right design decision?
Thanks in advance for your answers.

Recommended Answers

All 2 Replies

ddanbe,

That line occurs because of the flat style being standard. During the click, the component base uses the drop effect which drops the top border into view.
Avoid that in the onPaint (or elsewhere) so that it does not exhibit the drop on click effect.

base.FlatStyle = FlatStyle.Flat;

// Jerry

Jerry you're amazing! Would never have found that out by myself.
Thank you, thank you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.