it has text, an image, and then the checkbox,
I want to use a better image for the check, but cannot find a way to change the checked and unchecked images
this.checkBox1.CheckAlign = System.Drawing.ContentAlignment.MiddleRight;
this.checkBox1.Checked = true;
this.checkBox1.CheckState = System.Windows.Forms.CheckState.Checked;
this.checkBox1.Image = global::ClientExam.Properties.Resources.action32;
this.checkBox1.Location = new System.Drawing.Point(145, 140);
this.checkBox1.Name = "checkBox1";
this.checkBox1.Size = new System.Drawing.Size(273, 127);
this.checkBox1.TabIndex = 0;
this.checkBox1.Text = "checkBox1";
this.checkBox1.TextImageRelation = System.Windows.Forms.TextImageRelation.TextBeforeImage;
this.checkBox1.UseVisualStyleBackColor = true;
anybody know of one that doesn't require me to write my own control?
The solution is to use the :checked css selector. What we want to achieve is when you click the label (which will be an image or an icon) the input field will change to "checked" and the label image will change to something different.
If you are looking for how to do this in Winforms, the simple answer is to create a new checkbox class that derives from CheckBox, then override the OnPaint method.
Here is an example on how to create custom looking checkboxes by overriding the OnPaint
method:
public class CustomCheckBox : CheckBox
{
public CustomCheckBox()
{
this.SetStyle(ControlStyles.UserPaint, true);
this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
}
protected override void OnPaint(PaintEventArgs pevent)
{
base.OnPaint(pevent);
if (this.Checked)
{
pevent.Graphics.FillRectangle(new SolidBrush(Color.Blue), new Rectangle(0, 0, 16, 16));
}
else
{
pevent.Graphics.FillRectangle(new SolidBrush(Color.Red), new Rectangle(0, 0, 16, 16));
}
}
}
It's very simple, but it gives you the basic idea.
For anyone who'd prefer not to override OnPaint, there's an alternative solution:
ImageList
control and fill it with images to reflect checked/unchecked states.Checkbox
control's Appearance
property to Button
(to get rid of standard CheckBox icon)FlatStyle
property to Flat
(so that the control doesn't really look like a button).FlatAppearance
group of properties too. Namely CheckedBackColor
, MouseDownBackColor
, MouseOverBackColor
, i.e. set them all to Control
value.Checkbox
control's ImageList
property to the name of your ImageList
control.Checkbox
control's Imageindex
and ImageAlign
properties to reflect its' current state.Checkbox
control's TextImageRelation
property (this value won't let the text and image overlap unless you want them to). I.e. ImageBeforetext
value represents common CheckBox icon location.Now the only thing left to do is to change the image when the state is changed, smth like this:
private void chkMyCheckBoxWithAnImage_CheckedChanged(object sender, EventArgs e)
{
if (chkMyCheckBoxWithAnImage.Checked)
chkMyCheckBoxWithAnImage.ImageIndex = 1;
else
chkMyCheckBoxWithAnImage.ImageIndex = 0;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With