Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Toggle Button Control

Tags:

vb.net

Can I change my button control to toggle-button?

Is there any simple way to change the button property to make it toggle button?

like image 640
Furqan Sehgal Avatar asked Dec 12 '22 16:12

Furqan Sehgal


2 Answers

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.

like image 97
ChrisF Avatar answered Feb 11 '23 00:02

ChrisF


To Change Checkbox to Simple Latching On/Off Button

enter image description here

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

To Add Custom Toggle (Sliding) On/Off Switch

enter image description here

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

    enter image description here

  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 enter image description here
  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

like image 36
Automate This Avatar answered Feb 11 '23 01:02

Automate This