So here is my next creation, a circular checkbox. This is my third custom control, so I'm still learning, but have the basics just about down. Tips and suggestions are most appreciated:
class CircularCheckbox : Control
{
private Color borderColor;
public Color BorderColor
{
get { return borderColor; }
set { borderColor = value; }
}
private Color background;
public Color Background
{
get { return background; }
set { background = value; }
}
private Color tickColor;
public Color TickColor
{
get { return tickColor; }
set { tickColor = value; }
}
private bool isChecked;
public bool IsChecked
{
get { return isChecked; }
set { isChecked = value; }
}
public CircularCheckbox(bool IsChecked)
{
DoubleBuffered = true;
SetStyle(ControlStyles.SupportsTransparentBackColor, true);
this.BackColor = Color.Transparent;
this.IsChecked = IsChecked;
Background = Color.White;
BorderColor = Color.Black;
TickColor = Color.Green;
}
public CircularCheckbox()
: this(false)
{ }
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
Rectangle rc = this.ClientRectangle;
Graphics g = e.Graphics;
StringFormat sf = new StringFormat();
Font f = new Font("Arial", (float)rc.Height * 0.5f, FontStyle.Bold, GraphicsUnit.Pixel);
sf.Alignment = StringAlignment.Center;
sf.LineAlignment = StringAlignment.Center;
g.FillEllipse(new SolidBrush(Background), rc.Left + 1.5f, rc.Top + 1.5f, rc.Width - 4.0f, rc.Height - 4.0f);
g.DrawEllipse(new Pen(new SolidBrush(BorderColor), 3.0f), rc.Left + 1.5f, rc.Top + 1.5f, rc.Width - 4.0f, rc.Height - 4.0f);
if (IsChecked)
g.DrawString("\u2713", f, new SolidBrush(tickColor) , rc, sf);
}
protected override void OnClick(EventArgs e)
{
base.OnClick(e);
IsChecked = IsChecked ? false : true;
Invalidate();
}
}