Home > C# > Custom RadioButton

Custom RadioButton


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ViCleaner.controls
{
public partial class MGRadioButton : RadioButton
{
Image img = global::ViCleaner.Properties.Resources.uncheck;

public enum Status
{
check,uncheck
}

Status st = Status.check;

private void SetImg()
{
switch (st)
{
case Status.check:
img = global::ViCleaner.Properties.Resources.check;
break;
case Status.uncheck:
img = global::ViCleaner.Properties.Resources.uncheck;
break;
default:
img = global::ViCleaner.Properties.Resources.check;
break;

}
}

public MGRadioButton()
{
InitializeComponent();
this.Width = 200;
this.Height = img.Height;
this.Font = new System.Drawing.Font(“Palatino Linotype”, 26F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
}

protected override void OnPaint(PaintEventArgs pevent)
{
base.OnPaint(pevent);
Graphics g = pevent.Graphics;
g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAliasGridFit;
SetImg();
g.FillRectangle(new SolidBrush(SystemColors.Control), 0, 0, this.Width, this.Height);
g.DrawImage(img, 0, 0, img.Width, img.Height);
SizeF f = g.MeasureString(this.Text,this.Font);
g.DrawString(this.Text, this.Font, Brushes.Black, new PointF((img.Width +5), (img.Height – f.Height) / 2));
//TextRenderer.DrawText(g,this.Text, this.Font, new Rectangle(0, 0, this.Width + img.Width, this.Height), Color.FromArgb(0, 0, 0), TextFormatFlags.VerticalCenter | TextFormatFlags.HorizontalCenter);
}

protected override void OnCheckedChanged(EventArgs e)
{
if (this.Checked)
{

st = Status.check;
}
else
st = Status.uncheck;
//base.OnCheckedChanged(e);
//this.Refresh();
this.Invalidate();
}
//

protected override void OnClick(EventArgs e)
{
st = Status.check;
this.Checked = true;
//this.Refresh();
//base.OnClick(e);
this.Invalidate();
}

protected override void OnLayout(LayoutEventArgs levent)
{
if (this.Checked)
{

st = Status.check;
}
else
st = Status.uncheck;

//this.Refresh();
//base.OnLayout(levent);
this.Invalidate();
}
}
}

Categories: C#
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: