Solved

Radio Buttons and Event Handlers

Posted on 2014-02-21
4
587 Views
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?

Form
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

        Next

        For Each rb In GroupBox2.Controls

            AddHandler rb.CheckedChanged, AddressOf printerCheckChanged

        Next

        For Each rb In GroupBox3.Controls

            AddHandler rb.CheckedChanged, AddressOf printerCheckChanged

        Next

    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

0
Comment
Question by:Basicfarmer
[X]
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
4 Comments
 
LVL 80

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
0
 

Author Comment

by:Basicfarmer
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

                Next

            Next

        Next

Open in new window

0
 
LVL 86

Accepted Solution

by:
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
                Next
            Next
        Next

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.
0
 

Author Closing Comment

by:Basicfarmer
ID: 39878803
Great help guys, thanks...
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

726 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