Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

MultiPanelSwitch – How to Implement a Panel Switch Winforms Control

0.00/5 (No votes)
2 Apr 2020 1  
A panel switch replica, resembling electro-mechanical toggle of a switchboard box
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.

Image 1

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:

Image 2

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

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here