Mike Trethowan

Pen Truth Contributor

Over the years I have written several programs the rely on radio buttons for some function or another.  The first time that I worked with radio buttons the project had several of them in one group box.  After clicking on each one and having ten events to fill with code I thought that there has to be a better way and asked myself, “I wonder if I can have one event handle several Radio Button Checked Changed events.”

It didn’t seem that far fetched or unreasonable, after all they all use System.EventArgs.  So I tried it and it worked.  Now let me show you how I did it.

Start a new project and add a GroupBox and 4 RadioButtons to the GroupBox.  Select all the buttons and using Format from the Tool Bar, align the buttons so they look neat.  Next, with your mouse double click one of the RadioButtons, it really doesn’t mater which one.

Your form will open to the code page where you should see this:

Public Class frnMain

    Private Sub RadioButton1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton1.CheckedChanged

    End Sub

End Class

Copy the the code after “Handles”, RadioButton1.CheckedChanged.  Place a space after Handles, then add an underscore and press enter.  What you should have now is this:

Public Class frnMain

    Private Sub RadioButton1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles _
        RadioButton1.CheckedChanged

    End Sub

End Class

We are doing this for readability.  Add a comma to the end of RadioButton1.CheckedChanged, then paste a copy of  RadioButton1.CheckedChanged and add a comma.  Do this two more times, the last without a comma after the statement.  Next, edit three of the RadioButton1.CheckedChanged statements to represent all four RadioButtons as follows:

Public Class frnMain

    Private Sub RadioButton_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles _
        RadioButton1.CheckedChanged, RadioButton2.CheckedChanged, RadioButton3.CheckedChanged, RadioButton4.CheckedChanged

    End Sub

End Class

Finally, add the following code within the RadioButton_CheckedChanged sub.  Tip: You can rename the sub to any name that you like.  For this Quick Tip I simply removed the 1 since it will be handling all four buttons.   For variety, we don’t always have to have message boxes pop up, so I chose to change the background color of the form for this project.

       Select Case True
            Case RadioButton1.Checked
                Me.BackColor = Color.White
            Case RadioButton2.Checked
                Me.BackColor = Color.Turquoise
            Case RadioButton3.Checked
                Me.BackColor = Color.Tomato
            Case RadioButton4.Checked
                Me.BackColor = Color.Orchid
        End Select

Save and run the project.  Did the form come up to a color other than Control when it started?  That’s because at start up the program sets the default radio button checked to true which fires an event.  If this causes you problems, set the properties to False in the Properties window.  The next start up there should not be any radio buttons selected.

That’s it for this Quick Tip, I hope you found it helpful. For applying the same logic to a Form Button, Check out: “VB.NET Quick Tip – How To Form Button Hack

 

Disclaimer: The code in this tutorial is not intended as a final product or by no means the only way this project can be coded. The presented code is for educational purposes only and no warranty is granted or implied. I/we are not responsible for any damages as a result of using this code. Use at your own risk.

Comments are closed.