Mirel Vlad
Mirel Vlad

Reputation: 2082

How can I remove a CheckBox's borders?

I'd like to create a CheckBox without borders. It should still display the checkmark when checked.

Upvotes: 1

Views: 3847

Answers (3)

Mirel Vlad
Mirel Vlad

Reputation: 2082

Following AndreiV advice, I created a CustomControl which inherited from Label. Next I override the OnPaint and OnClick events to make it look and behave like a CheckBox. In order to display the check box "checked image" I used a bit of paint for cropping it to what I needed.

Below is the full code:

public partial class FlatCheckBox : Label
{
    public bool Checked { get; set; }

    public FlatCheckBox()
    {
        InitializeComponent();
        Checked = false;
    }

    protected override void OnClick(EventArgs e)
    {
        Checked = !Checked;
        Invalidate();
    }

    protected override void OnPaint(PaintEventArgs pevent)
    {
        if (!DesignMode)
        {
            pevent.Graphics.Clear(Color.White);

            var bigRectangle = new Rectangle(pevent.ClipRectangle.X, pevent.ClipRectangle.Y,
                                             pevent.ClipRectangle.Width, pevent.ClipRectangle.Height);
            var smallRectangle = new Rectangle(pevent.ClipRectangle.X + 1, pevent.ClipRectangle.Y + 1,
                                               pevent.ClipRectangle.Width - 2, pevent.ClipRectangle.Height - 2);

            var b = new SolidBrush(UllinkColors.NEWBLUE);
            var b2 = new SolidBrush(Color.White);

            pevent.Graphics.FillRectangle(b, bigRectangle);
            pevent.Graphics.FillRectangle(b2, smallRectangle);

            if (Checked)
            {
                pevent.Graphics.DrawImage(Resources.flatCheckedBox, new Point(3, 3));
            }
        }
    }
}

Upvotes: 0

chouaib
chouaib

Reputation: 2817

using System;
using System.Collections.Generic;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
using System.Linq;
using System.Text;

namespace WindowsFormsApplication1
{
    public class RoundButton : Button
    {

        protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
        {
            GraphicsPath grPath = new GraphicsPath();
            grPath.AddEllipse(0, 0, ClientSize.Width, ClientSize.Height);
            this.Region = new System.Drawing.Region(grPath);
            base.OnPaint(e);
        }

        private void InitializeComponent()
        {
            this.SuspendLayout();
            this.ResumeLayout(false);

        }
    }

}

this is a class that generate a customized round button, can be good starter for you to make your own customCheckbox by doing similarly

Upvotes: 3

nestedloop
nestedloop

Reputation: 2656

The verified answer on this question states:

You cannot remove just the border because the check box is drawn by windows and it's pretty much all or nothing.

This is because the System.Windows.CheckBox is a native control.

A workaround to this would be drawing you own CustomCheckBox, with no visible borders.

Hope this helps.

Upvotes: 0

Related Questions