Toggle switch control in Windows Forms

Toggle switch control in Windows Forms

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.

Examples

  1. "Windows Forms toggle switch control"

    • Description: Search for a Windows Forms toggle switch control to implement a simple on/off switch in your application.
    • Code:
      // 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();
      
  2. "Windows Forms toggle switch event handling"

    • Description: Learn how to handle events (e.g., CheckedChanged) associated with a toggle switch in Windows Forms.
    • Code:
      // Code for handling CheckedChanged event in a Windows Forms toggle switch
      toggleSwitch.CheckedChanged += (sender, e) =>
      {
          MessageBox.Show($"Toggle switch is {(toggleSwitch.Checked ? "ON" : "OFF")}");
      };
      
  3. "Customizable toggle switch in Windows Forms"

    • Description: Find ways to create a customizable toggle switch in Windows Forms with options for color, size, and other visual elements.
    • Code:
      // 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();
      
  4. "Windows Forms toggle switch binding"

    • Description: Understand how to bind a Windows Forms toggle switch control to a data source or property.
    • Code:
      // 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; }
      }
      
  5. "Windows Forms toggle switch tooltip"

    • Description: Add a tooltip to a Windows Forms toggle switch control to provide additional information or context.
    • Code:
      // Code for adding a tooltip to a Windows Forms toggle switch
      toggleSwitch.ToolTipText = "Toggle the switch on/off";
      
  6. "Windows Forms toggle switch keyboard accessibility"

    • Description: Implement keyboard accessibility for a Windows Forms toggle switch to ensure a seamless user experience.
    • Code:
      // 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;
          }
      };
      
  7. "Windows Forms toggle switch with status indicator"

    • Description: Enhance a Windows Forms toggle switch by adding a visual status indicator or label to convey additional information.
    • Code:
      // 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();
      

More Tags

ssrs-2012 samsung-galaxy powerset webcam-capture composite-primary-key android-3.0-honeycomb icalendar tostring saga layout

More C# Questions

More Date and Time Calculators

More Everyday Utility Calculators

More Auto Calculators

More Fitness-Health Calculators