In this article, I will show how to program and implement a new custom control in VB.NET, and use it in a virtual switchboard application.
Introduction
How many times in your childhood memories did you see those black, selectors on switchboards, for operating machinery or to activate heavy equipment. In the old times, it was not unusual to have burners, boilers and air conditioning equipment activated with two- or three-state switches.
For those who miss those times, here’s a free panel switch for your Winforms applications. Instantiate the control in your application by adding it as a new project, or just compile the DLL and reference it.
It already includes a demo app, that shows how to properly use the control.
MultiPanelSwitch
Clicking the Control
What happens if I click on the control? The switch will toggle, either ON/OFF if 2-position, or ON/AUTO/OFF if required 3-positions.
Private Sub Me_click(sender As Object, e As EventArgs) Handles pbSwitch.Click
m_selectedPosition += 1
If m_selectedPosition > m_positions - 1 Then
m_selectedPosition = 0
angle = -3 / 4 * Pi
Me.Refresh()
Exit Sub
End If
If m_positions = 2 Then
angle += Pi / 2
Else
angle += Pi / 4
End If
Me.Refresh()
End Sub
Upon Me.Refresh()
, Private Sub GaugePaint(sender As Object, e As PaintEventArgs) Handles Me.Paint
is called. In GaugePaging
, the whole control is redrawn, based on the properties set to it.
Properties of the Control
Public Property isSemaphorVisible As Boolean
When set to True
, the control gets a signalization lamp, to be used as status lamp or else, as shown below:
Public Property isSemaphorBlinking As Boolean
When set to True
, the lamp will start blinking, if set to ON. This is useful for alarm signalization.
Public Property SemaphorColor As Color
The color of the status lamp! Can be amber, blue, green, red or off, for warnings, alarms, status control and so on.
Public Property positions As Integer
The switch can be set to either two or three-states.
Public Property lbltext As String and Public Property semaphortext As String
The two properties for setting the texts. In the image above, lbltext
is “Pump 1” and semaphortext
is “Status”.
As a courtesy to the programmer, the control returns what position is actually selected, by calling the readonly
property selectedPosition
. It can be either 1 or 2 in a 2-position switch, or 1, 2 and 3 in a 3-position switch.
How to Use It?
Drag and drop the control on your form, and it will be automatically instantiated and initialized with default values.
Alternatively, it is possible to instantiate it at design time:
Friend WithEvents MultiPanelSwitch4 As MultiPanelSwitch.MultiPanelSwitch
Controls.Add(Me.MultiPanelSwitch4)
In the included demo app, we show how to press a button and set the semaphor blinking a red warning:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If MultiPanelSwitch4.semaphorColor = Color.Red Then
MultiPanelSwitch4.semaphorColor = Color.Black
Else
MultiPanelSwitch4.semaphorColor = Color.Red
End If
MultiPanelSwitch4.isSemaphorBlinking = Not MultiPanelSwitch4.isSemaphorBlinking
End Sub
Conclusion
It is very easy to write a user custom control for Winforms. In this case, replicate a toggle switch for nostalgia of machinery switchboards. Learn how to use my user control and use it freely in your steampunk desktop applications.
History
- 28th March, 2020: Initial version