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.
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.
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
If m_positions = 2 Then
angle += Pi / 2
angle += Pi / 4
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
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
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
MultiPanelSwitch4.semaphorColor = Color.Red
MultiPanelSwitch4.isSemaphorBlinking = Not MultiPanelSwitch4.isSemaphorBlinking
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.
- 28th March, 2020: Initial version