[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now


Radio Buttons and Event Handlers

Posted on 2014-02-21
Medium Priority
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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
LVL 83

Assisted Solution

by:David Johnson, CD, MVP
David Johnson, CD, MVP earned 1000 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 86

Accepted Solution

Mike Tomlinson earned 1000 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

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

The purpose of this article is to demonstrate how we can use conditional statements using Python.
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
The viewer will learn how to implement Singleton Design Pattern in Java.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

650 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