There is no built-in toggle switch control in Windows Forms, but you can create a custom control that behaves like a toggle switch using a CheckBox
control and some custom drawing. Here's an example implementation:
using System.Drawing; using System.Windows.Forms; public class ToggleSwitch : CheckBox { private Color _onColor = Color.Green; private Color _offColor = Color.Gray; public Color OnColor { get { return _onColor; } set { _onColor = value; } } public Color OffColor { get { return _offColor; } set { _offColor = value; } } protected override void OnPaint(PaintEventArgs e) { var g = e.Graphics; var switchRectangle = new Rectangle(0, 0, Height * 2, Height); var switchBorderPen = new Pen(Color.Black, 2); var switchFillBrush = new SolidBrush(Checked ? OnColor : OffColor); var thumbRectangle = new Rectangle(0, 0, Height - 4, Height - 4); var thumbBorderPen = new Pen(Color.Black, 1); var thumbFillBrush = new SolidBrush(Color.White); g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias; g.FillRectangle(switchFillBrush, switchRectangle); g.DrawRectangle(switchBorderPen, switchRectangle); if (Checked) { thumbRectangle.X = switchRectangle.Width - thumbRectangle.Width - 2; } else { thumbRectangle.X = 2; } g.FillEllipse(thumbFillBrush, thumbRectangle); g.DrawEllipse(thumbBorderPen, thumbRectangle); } }
In this implementation, we define a custom ToggleSwitch
control that inherits from CheckBox
. We override the OnPaint
method to perform custom drawing of the switch and the thumb. The OnColor
and OffColor
properties allow the colors of the switch to be customized.
To use this control, simply add it to your Windows Form like any other control. You can then handle its CheckedChanged
event to perform some action when the switch is toggled.
"Windows Forms toggle switch control"
// Code for a simple toggle switch control in Windows Forms using System.Windows.Forms; public class ToggleSwitch : CheckBox { public ToggleSwitch() { Appearance = Appearance.Button; Text = "OFF"; } protected override void OnCheckedChanged(EventArgs e) { base.OnCheckedChanged(e); Text = Checked ? "ON" : "OFF"; } } // Usage var toggleSwitch = new ToggleSwitch();
"Windows Forms toggle switch event handling"
// Code for handling CheckedChanged event in a Windows Forms toggle switch toggleSwitch.CheckedChanged += (sender, e) => { MessageBox.Show($"Toggle switch is {(toggleSwitch.Checked ? "ON" : "OFF")}"); };
"Customizable toggle switch in Windows Forms"
// Code for a customizable toggle switch in Windows Forms public class CustomToggleSwitch : CheckBox { public CustomToggleSwitch() { Appearance = Appearance.Button; Text = "OFF"; // Additional customization code here } } // Usage var customToggleSwitch = new CustomToggleSwitch();
"Windows Forms toggle switch binding"
// Code for binding a Windows Forms toggle switch to a data source or property var bindingSource = new BindingSource(); bindingSource.DataSource = typeof(ToggleSwitchData); var toggleSwitch = new ToggleSwitch(); toggleSwitch.DataBindings.Add("Checked", bindingSource, "IsToggled"); // Define a class for data binding public class ToggleSwitchData { public bool IsToggled { get; set; } }
"Windows Forms toggle switch tooltip"
// Code for adding a tooltip to a Windows Forms toggle switch toggleSwitch.ToolTipText = "Toggle the switch on/off";
"Windows Forms toggle switch keyboard accessibility"
// Code for keyboard accessibility in a Windows Forms toggle switch toggleSwitch.KeyDown += (sender, e) => { if (e.KeyCode == Keys.Space) { toggleSwitch.Checked = !toggleSwitch.Checked; e.Handled = true; } };
"Windows Forms toggle switch with status indicator"
// Code for a toggle switch with a status indicator in Windows Forms public class StatusIndicatorToggleSwitch : ToggleSwitch { private readonly Label statusLabel; public StatusIndicatorToggleSwitch() { statusLabel = new Label { Text = "Status: OFF", AutoSize = true, Location = new Point(Right + 10, Top) }; Controls.Add(statusLabel); CheckedChanged += (sender, e) => { statusLabel.Text = $"Status: {(Checked ? "ON" : "OFF")}"; }; } } // Usage var statusIndicatorToggleSwitch = new StatusIndicatorToggleSwitch();
ssrs-2012 samsung-galaxy powerset webcam-capture composite-primary-key android-3.0-honeycomb icalendar tostring saga layout