-
Feb 24th, 2023, 02:09 PM
#1
OnOffControl
Here's a graphical OnOffControl for VB.Net...
These are the images in my.resources..
(off image)
(on image)
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.
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Feb 24th, 2023, 02:46 PM
#2
Hyperactive Member
Re: OnOffControl
Beautifully done...👏 Accept my warm appreciation far away from the kingdom.
-
Feb 24th, 2023, 03:07 PM
#3
Re: OnOffControl
Originally Posted by pourkascheff
Beautifully done...👏 Accept my warm appreciation far away from the kingdom.
Thanks
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Feb 26th, 2023, 10:16 AM
#4
Re: OnOffControl
Originally Posted by pourkascheff
Beautifully done...👏 Accept my warm appreciation far away from the kingdom.
@pourkascheff...
I edited the code. The improved version is above
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Feb 26th, 2023, 10:52 AM
#5
Re: OnOffControl
Had another think about this OnOff_Control...
Added Enabled/Disabled support
(off image)
(on image)
(disabled image)
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.
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|