Here's a graphical OnOffControl for VB.Net...
These are the images in my.resources..
(off image)
(on image)
See the latest (best) version in post #5Code: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




Reply With Quote