Radio Buttons and Event Handlers

Posted on 2014-02-21
Last Modified: 2014-02-21
Gentlemen, please see the attached form screen shot. I think it will go a long ways in explaining what i am talking about. In my project i have a form with three group boxes and in those group boxes i have several radio buttons. There is also a listview on the form. In my form i have a method called "fillListview". Each time a radio button is selected it calls this method to populate the listview with the data defined by the radio buttons. Obviously i dont want to call the "fillListView" method from the checkedChanged event of every radio button. So i wired all those events to a single event handler.

This next part is what i think happened. When the form loaded even though i set the checked property to True in the designer, it appears that the event fires before the form is loaded because when the radio button object is created it gets set to true thus firing the checkedChanged event because it changed from false or nothing to a true state.

So i got rid of the event handlers and just focused on the radio buttons in the printers groupbox. I found that my "fillListView" method was getting called twice. Then i realized it was firing when the state of one radio button changed from true to false and then fired again when the state of the next radio button changed from false to true.

So after some research the code below is what i came up with. It seems to work the way i want it. So really what i am wondering is do i understand this correctly regarding the event handlers and the "fillListView" method being called twice? Also, are there any alternative concepts to this? Is this the correct way to handle what i am trying to accomplish? How would you approach this?

The code below is what i am doing to get this working.

When the form loads I add the event handlers.
    Private Sub equipment_Load(sender As Object, e As System.EventArgs) Handles Me.Load

        loadColumns(Me.Name, ListView1)

        Dim rb As RadioButton

        For Each rb In GroupBox1.Controls

            AddHandler rb.CheckedChanged, AddressOf printerCheckChanged


        For Each rb In GroupBox2.Controls

            AddHandler rb.CheckedChanged, AddressOf printerCheckChanged


        For Each rb In GroupBox3.Controls

            AddHandler rb.CheckedChanged, AddressOf printerCheckChanged


    End Sub

Open in new window

My event.
    Private Sub printerCheckChanged(sender As System.Object, e As EventArgs)

        If CType(sender, RadioButton).Checked = False Then Exit Sub Else fillListView()

    End Sub

Open in new window

Question by:Basicfarmer
  • 2
LVL 79

Assisted Solution

by:David Johnson, CD, MVP
David Johnson, CD, MVP earned 250 total points
ID: 39878124
Yet another way
  Private Sub RadioButton9_CheckedChanged(sender As System.Object, e As System.EventArgs) Handles RadioButton9.CheckedChanged
        If RadioButton9.Checked = True Then
            Label3.Text = "Left-Hand"
        Else : Label3.Text = ""
        End If
    End Sub

Open in new window

Sample Code

Author Comment

ID: 39878183
I have generated a lot of statements to add event handlers for each individual groupbox. So i am trying to do the event handlers this way. But cannot get it to work.
        Dim gb As GroupBox
        Dim rb As RadioButton
        Dim tp As TabPage

        For Each tp In TabControl1.TabPages

            For Each gb In tp.Controls

                For Each rb In gb.Controls

                    AddHandler rb.CheckedChanged, AddressOf checkedChanged




Open in new window

LVL 85

Accepted Solution

Mike Tomlinson earned 250 total points
ID: 39878489
That attempt will fail whenever a control is encountered that is not of the type you declared  (groupbox/radiobutton).  Here is one way to fix it:
        For Each tp As TabPage In TabControl1.TabPages
            For Each gb As GroupBox In tp.Controls.OfType(Of GroupBox)()
                For Each rb As RadioButton In gb.Controls.OfType(Of RadioButton)()
                    AddHandler rb.CheckedChanged, AddressOf checkedChanged

Open in new window

*Note that I declared the types in the For Each statements so you don't need the separate declarations beforehand anymore.

Author Closing Comment

ID: 39878803
Great help guys, thanks...

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

815 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now