Results 1 to 5 of 5

Thread: OnOffControl

  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.

  2. #2
    Hyperactive Member pourkascheff's Avatar
    Join Date
    Apr 2020
    Location
    LocalHost
    Posts
    381

    Re: OnOffControl

    Beautifully done...👏 Accept my warm appreciation far away from the kingdom.

  3. #3

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

    Re: OnOffControl

    Quote Originally Posted by pourkascheff View Post
    Beautifully done...👏 Accept my warm appreciation far away from the kingdom.
    Thanks

  4. #4

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

    Re: OnOffControl

    Quote Originally Posted by pourkascheff View Post
    Beautifully done...👏 Accept my warm appreciation far away from the kingdom.
    @pourkascheff...

    I edited the code. The improved version is above

  5. #5

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

    Re: OnOffControl

    Had another think about this OnOff_Control...

    Added Enabled/Disabled support

    (off image)

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

    (on image)

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

    (disabled image)

    Name:  disabled.png
Views: 895
Size:  612 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
            disabled = 4
        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)
                ElseIf (value And CtrlState.disabled) = CtrlState.disabled Then
                    Me.BackgroundImage = My.Resources._disabled
                    button.Visible = False
                End If
                RaiseEvent Status_Changed()
            End Set
        End Property
    
    
        Public Shadows Property Enabled() As Boolean
            Get
                Return MyBase.Enabled
            End Get
            Set(ByVal value As Boolean)
                MyBase.Enabled = value
                If MyBase.Enabled = False Then
                    Me.State = CtrlState.disabled
                Else
                    button.Visible = True
                    Me.State = CtrlState.off
                End If
            End Set
        End Property
    
    
        Dim minLeft As Integer = 8
        Dim maxLeft As Integer = 40
        Dim midPointX As Integer = 24
        Dim startPoint As Point
    
        Public Sub New()
            Me.Size = My.Resources._on.Size
            Me.MaximumSize = Me.Size
            Me.MinimumSize = Me.Size
            Me.DoubleBuffered = True
    
            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)
                If button.Left <= midPointX Then
                    Me.BackgroundImage = My.Resources._off
                Else
                    Me.BackgroundImage = My.Resources._on
                End If
            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
    Last edited by .paul.; Feb 27th, 2023 at 04:28 AM.

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