Results 1 to 5 of 5

Thread: OnOffControl

Threaded View

  1. #1

    Thread Starter
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,689

    OnOffControl

    Here's a graphical OnOffControl for VB.Net...

    Name:  24-02-2023_19.04.31.png
Views: 939
Size:  3.8 KB

    Name:  24-02-2023_19.04.24.png
Views: 874
Size:  3.8 KB

    These are the images in my.resources..

    (off image)

    Name:  off.png
Views: 931
Size:  768 Bytes

    (on image)

    Name:  on.png
Views: 933
Size:  727 Bytes

    Code:
    Public Class OnOff_Control
        Inherits Panel
    
        Public Event Status_Changed()
    
        Private button As New Panel With {.Size = New Size(16, 16), .BackColor = Color.Black}
    
        Enum CtrlState
            [on] = 1
            off = 2
        End Enum
    
        Private _State As CtrlState
        Public Property State() As CtrlState
            Get
                Return _State
            End Get
            Set(ByVal value As CtrlState)
                _State = value
                If (value And CtrlState.on) = CtrlState.on Then
                    Me.BackgroundImage = My.Resources._on
                    button.Location = New Point(maxLeft, 8)
                ElseIf (value And CtrlState.off) = CtrlState.off Then
                    Me.BackgroundImage = My.Resources._off
                    button.Location = New Point(minLeft, 8)
                End If
                RaiseEvent Status_Changed()
            End Set
        End Property
    
        Dim minLeft As Integer = 8
        Dim maxLeft As Integer = 40
        Dim midPointX As Integer = 32
        Dim startPoint As Point
    
        Public Sub New()
            Me.Size = My.Resources._on.Size
            Me.Controls.Add(button)
            Dim gp As New Drawing2D.GraphicsPath
            gp.AddEllipse(New Rectangle(0, 0, 16, 16))
            button.Region = New Region(gp)
            AddHandler button.MouseDown, AddressOf button_MouseDown
            AddHandler button.MouseMove, AddressOf button_MouseMove
            AddHandler button.MouseUp, AddressOf button_MouseUp
            Me.State = CtrlState.off
        End Sub
    
        Private Sub button_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
            startPoint = New Point(e.X, 8)
            Me.Cursor = Cursors.Hand
        End Sub
    
        Private Sub button_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
            If e.Button = Windows.Forms.MouseButtons.Left Then
                Dim newX As Integer = button.Location.X + e.X - startPoint.X
                If newX < minLeft Then newX = minLeft
                If newX > maxLeft Then newX = maxLeft
                button.Location = New Point(newX, 8)
            End If
        End Sub
    
        Private Sub button_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
            startPoint = Nothing
            Me.Cursor = Cursors.Default
            If button.Left <= midPointX Then
                Me.State = CtrlState.off
            Else
                Me.State = CtrlState.on
            End If
        End Sub
    
        Protected Overrides Sub OnMouseDown(ByVal e As System.Windows.Forms.MouseEventArgs)
            If e.Button = Windows.Forms.MouseButtons.Left Then
                If e.Y > 4 And e.Y < Me.Height - 4 And e.X > 4 And e.X < Me.Width - 4 And e.X > midPointX Then
                    Me.State = CtrlState.on
                ElseIf e.Y > 4 And e.Y < Me.Height - 4 And e.X > 4 And e.X < Me.Width - 4 And e.X <= midPointX Then
                    Me.State = CtrlState.off
                End If
            End If
            MyBase.OnMouseDown(e)
        End Sub
    
    End Class
    See the latest (best) version in post #5
    Last edited by .paul.; Mar 4th, 2023 at 08:18 PM.

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