Click here to Skip to main content
14,548,943 members

MultiPanelSwitch – How to Implement a Panel Switch Winforms Control

Rate this:
5.00 (4 votes)
Please Sign up or sign in to vote.
5.00 (4 votes)
2 Apr 2020CPOL
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Padanian
Italy Italy
No Biography provided

Comments and Discussions

 
GeneralMy vote of 5 Pin
Member 123643902-Apr-20 21:43
MemberMember 123643902-Apr-20 21:43 
QuestionSource code missing Pin
Padanian2-Apr-20 10:47
MemberPadanian2-Apr-20 10:47 
Questionsource code location Pin
ASK20102-Apr-20 3:39
professionalASK20102-Apr-20 3:39 
QuestionGreat Article . I will use this for my automation projects. :) Pin
Niccata2-Apr-20 1:40
MemberNiccata2-Apr-20 1:40 
Questiondemo application and code Pin
Member 101955051-Apr-20 6:47
professionalMember 101955051-Apr-20 6:47 
Questionsource code Pin
ARIA681-Apr-20 1:42
professionalARIA681-Apr-20 1:42 
AnswerRe: source code Pin
Member 143296421-Apr-20 22:56
MemberMember 143296421-Apr-20 22:56 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Tip/Trick
Posted 31 Mar 2020

Tagged as

Stats

3.6K views
27 downloads
6 bookmarked