Custom checkbox

ddanbe 0 Tallied Votes 2K Views Share

I wanted to make an array of bits which I could click on and off at will. Sort of a register you could manipulate. First thing that came up was a checkbox control. But I wanted to change the usual checkmark with a 1 or a 0. With tips from JerryShaw and Ramy Marhous I came up with the following.

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);          
        }
    }

  class BinarycheckBox : CheckBox
        //class derived from the CheckBox class
    {
        //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 only need to override the OnPaint method 
        //we do our own painting here
        protected override void OnPaint(PaintEventArgs pevent)
        {
            base.OnPaint(pevent);   //needed
            base.FlatStyle = FlatStyle.Flat; //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, strFormat);
            else
                G.DrawString("0", font, bluebrush, R, strFormat);
        
            G.DrawRectangle(bluepen, R.X, R.Y, R.Width - 1, R.Height - 1);          
        }
    }
kingk110 0 Newbie Poster

it is causing visual studio to be not responding if you add the checkbox from the toolbox to the form and not from the command this.Controls.Add(checkbox)
do you have anyidea about it?

ddanbe 2,724 Professional Procrastinator Featured Poster

Are you sure this causes your problem?
Always has worked when I used it.
Have you tried to debug?
What EXACLY happens when you use it?

TnTinMN 418 Practically a Master Poster

I got some funky behavior as well. Had two controls on the form and one of then tended to disappear.

I think it was related to you setting the FlatStyle in OnPaint. Changing styles tends to trigger a paint event on most controls.

I modified it as follows and all worked great.

//default constructor
    public BinarycheckBox() 
    {
     base.FlatStyle  =  FlatStyle.Flat; //needed
    }

    [System.ComponentModel.DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
    public new System.Windows.Forms.FlatStyle FlatStyle
    {
        get { return base.FlatStyle; }
        set { }
    }

    //we only need to override the OnPaint method 
    //we do our own painting here
    protected override void OnPaint(PaintEventArgs pevent)
    {
        base.OnPaint(pevent);   //needed
ddanbe commented: Looks great! I fact it was with FlatStyle I had troubles with! +14
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.