Solved

Radio Buttons and Event Handlers

Posted on 2014-02-21
4
558 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
  • 2
4 Comments
 
LVL 78

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 85

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This article will show, step by step, how to integrate R code into a R Sweave document
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
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 learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

760 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

18 Experts available now in Live!

Get 1:1 Help Now