Results 1 to 2 of 2

Thread: Toggle Switch Button

  1. #1

    Thread Starter
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,711

    Toggle Switch Button

    I wanted to make something like you're able to do in WPF or with HTML/CSS for a Windows Form Application in regards to a toggle switch button, so here is my attempt at a "Window's Theme" toggle switch button for a windows form application:
    Code:
    Option Strict On
    Option Explicit On
    
    Public Class Toggle
        Inherits System.Windows.Forms.Control
    
        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)
            'Draw a transparent background
            Using parentColor As Pen = New Pen(Me.Parent.BackColor, 10)
                e.Graphics.DrawRectangle(parentColor, Me.DisplayRectangle)
            End Using
    
            'Draw the "slider" border
            ControlPaint.DrawBorder(e.Graphics, New Rectangle(5, 5, Me.Width - 10, Me.Height - 10), SystemColors.ControlDark, ButtonBorderStyle.Solid)
    
            'Draw the "grip"
            ControlPaint.DrawGrabHandle(e.Graphics, New Rectangle(If(Me.Checked, 0, Me.Width - 25), 0, 25, Me.Height), True, True)
    
            MyBase.OnPaint(e)
        End Sub
    
    End Class
    Last edited by dday9; Apr 12th, 2017 at 12:49 PM.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  2. #2
    Fanatic Member
    Join Date
    Oct 2011
    Location
    Sydney, Australia
    Posts
    756

    Re: Toggle Switch Button

    Nice work! I have one like this in the codebank as well, feel free to borrow code if you like it

    http://www.vbforums.com/showthread.p...-Slide-control
    My CodeBank Submissions
    • Listbox with transparency and picture support - Click Here
    • Check for a true internet connection - Click Here
    • Open Cash drawer connected to receipt printer - Click Here
    • Custom color and size border around form - Click Here
    • Upload file to website without user logins, includes PHP - Click Here
    • List All Removable USB Storage Devices - Click Here
    • Custom On/Off Slide Control - Click Here
    • Insert multiple rows of data into one database table using parameters - Click Here
    • Trigger USB/Serial Cash Drawer - Click Here

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width