开发者

Toggle Button Control

开发者 https://www.devze.com 2023-02-06 09:36 出处:网络
Can I change my button control to toggle-button? Is there开发者_StackOverflow any simple way to change the button property to make it toggle button?According to this post on OSIX all you need to do i

Can I change my button control to toggle-button?

Is there开发者_StackOverflow any simple way to change the button property to make it toggle button?


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.


To Change Checkbox to Simple Latching On/Off Button

Toggle Button Control

myCheckBox.Appearance = System.Windows.Forms.Appearance.Button

To Add Custom Toggle (Sliding) On/Off Switch

Toggle Button Control

  1. Right click project in VS and select 'Add' then 'User Control...'

    Toggle Button Control

  2. Name your new file "Toggle.vb"

  3. Paste the code below
  4. Switch to your form and drag your 'toggle' control from toolbox to form

    Toggle Button Control

  5. Size & settings can be changed like standard control
  6. Colors can be changed in OnPaint method of Toggle Class

VB.net

Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Windows.Forms

Public Class Toggle
    Inherits System.Windows.Forms.UserControl

    Private _checked As Boolean
    Public Property Checked As Boolean
        Get
            Return _checked
        End Get
        Set(ByVal value As Boolean)
            If Not _checked.Equals(value) Then
                _checked = value
                Me.OnCheckedChanged()
            End If
        End Set
    End Property

    Protected Overridable Sub OnCheckedChanged()
        RaiseEvent CheckedChanged(Me, EventArgs.Empty)
    End Sub

    Public Event CheckedChanged(ByVal sender As Object, ByVal e As EventArgs)

    Protected Overrides Sub OnMouseClick(e As MouseEventArgs)
        Me.Checked = Not Me.Checked
        Me.Invalidate()
        MyBase.OnMouseClick(e)
    End Sub

    Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
        Me.OnPaintBackground(e)
        e.Graphics.SmoothingMode = SmoothingMode.AntiAlias

        Using path = New GraphicsPath()
            Dim d = Padding.All
            Dim r = Me.Height - 2 * d
            path.AddArc(d, d, r, r, 90, 180)
            path.AddArc(Me.Width - r - d, d, r, r, -90, 180)
            path.CloseFigure()
            e.Graphics.FillPath(If(Checked, Brushes.DarkGray, Brushes.LightGray), path)
            r = Height - 1
            Dim rect = If(Checked, New System.Drawing.Rectangle(Width - r - 1, 0, r, r), New System.Drawing.Rectangle(0, 0, r, r))
            e.Graphics.FillEllipse(If(Checked, Brushes.Green, Brushes.LightSlateGray), rect)
        End Using
    End Sub
End Class

C#

using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;

namespace Your_Project_Name
{
    class Toggle : CheckBox
    {
        public Toggle()
        {
            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 System.Drawing.Rectangle(Width - r - 1, 0, r, r)
                                    : new System.Drawing.Rectangle(0, 0, r, r);
                e.Graphics.FillEllipse(Checked ? Brushes.Green : Brushes.LightSlateGray, rect);
            }
        }
    }
}

This code is from several sources over the years and has some minor tweaks. It appears it exist in various forms on different sites so it's unclear who to attribute

All code was tested in Visual Studio 2017


If i understood correctly, you can achieve this functionality by using a flag. For example: bool isClicked = false;

and in the clicked event to put the following code to invert the bool: isClicked = !isClicked;

0

精彩评论

暂无评论...
验证码 换一张
取 消