ToggleButton in C# WinForms
I ended up overriding the OnPaint and OnBackgroundPaint events and manually drawing the button exactly like I need it. It worked pretty well.
Simple ON/OFF toggle button with image
Change your code to:
private void ToggleButton_CheckedChanged(object sender, EventArgs e)
{
if (ToggleButton.Checked)
ToggleButton.BackgroundImage = Properties.Resources.ToggleButton_ON;
else
ToggleButton.BackgroundImage = Properties.Resources.ToggleButton_OFF;
}
The .Equals is for checking equality which you can override in your own classes.
Winform image toggle button
The usual way is to use a CheckBox
with Appearance=Button
.
You can toggle its ImageIndex
and Text
in the CheckedChanged
event.
You need to associate it with a well-prepared ImageList
of the right ImageSize
an ColorDepth
.
You can get away with ca 3 lines of code:
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
if (checkBox1.Checked)
{ checkBox1.ImageIndex = 1; checkBox1.Text = "Sue"; }
else
{ checkBox1.ImageIndex = 2; checkBox1.Text = "Ellen"; }
}
Toggle Button Control
According to this post on OSIX all you need to do is use a CheckBox
but set it's appearance to Button
.
In code:
CheckBox checkBox1 = new System.Windows.Forms.CheckBox();
checkBox1.Appearance = System.Windows.Forms.Appearance.Button;
(C# code but you see how it works).
But you can do this from the Properties dialog in the designer.
Buttons in WinForms
You can make checkbox or radiobutton look like a Button
cb.Appearance = Appearance.Button;
Toggle switch control in Windows Forms
I know this is a Windows Forms question. But you may want to take a look at Toggle Switches or read more about Universal Windows App Components.
Anyway, here is an answer for Windows Forms developers. It shows how we can customize rendering of a checkbox to have such appearance.
Currently you are drawing only an ellipse, and it's quite a toggle button. But if you want to show it like the below image, you should first draw a round shape for background, and then based on the Checked
value, draw the check circle. Using the code in Example part of the answer you can have a CheckBox
with such a UI:
Example
The important thing about this sample is it's completely a CheckBox
control and supports check using mouse and keyboard. It also supports data-binding and all other standard features of CheckBox
. The code is not perfect, but it is a good start point to have a yes/no toggle switch:
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
public class MyCheckBox : CheckBox
{
public MyCheckBox()
{
SetStyle(ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint, true);
Padding = new Padding(6);
}
protected override void OnPaint(PaintEventArgs e)
{
this.OnPaintBackground(e);
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
using (var path = new GraphicsPath())
{
var d = Padding.All;
var r = this.Height - 2 * d;
path.AddArc(d, d, r, r, 90, 180);
path.AddArc(this.Width - r - d, d, r, r, -90, 180);
path.CloseFigure();
e.Graphics.FillPath(Checked ? Brushes.DarkGray : Brushes.LightGray, path);
r = Height - 1;
var rect = Checked ? new Rectangle(Width - r - 1, 0, r, r)
: new Rectangle(0, 0, r, r);
e.Graphics.FillEllipse(Checked ? Brushes.Green : Brushes.WhiteSmoke, rect);
}
}
}
Related Topics
Cannot Close Excel.Exe After Interop Process
Matching Strings with Wildcard
.Net Core 2.2 Can't Be Selected in Visual Studio Build Framework
Getting Serial Port Information
How to Compare Values of Generic Types
How to Invoke a UI Method from Another Thread
How to Select Min and Max Values of a Column in a Datatable
Send HTML Email via C# with Smtpclient
How to Get a List of All Routes in ASP.NET Core
How to Use Use Late Binding to Get Excel Instance
What's the How to Minimize to Tray a C# Winforms App
How to Delete a Directory with Read-Only Files in C#
Console.Writeline and Generic List
Single App.Config Multi-Project C#