How to check is any radio button selected to change his forecolor

topsykretts

Well-known member
Joined
Dec 22, 2011
Messages
47
Programming Experience
Beginner
Hi friends, I have a problem to use "For" statement on more than 10 radio buttons on my form.

Sample for one radio button:
If Form2.RadioButton1.Checked = True Then
Form2.RadioButton1.ForeColor = Color.Red
Else
Form2.RadioButton1.ForeColor = Color.Yellow
End If

But if I want to use this on any radiobutton on my form, I will use something like this;



VB.NET:
        Dim i As Integer
        For i = 1 To 10
            If Form2.RadioButton(i).Checked = True Then
                Form2.RadioButton(i).ForeColor = Color.Red
            Else
                Form2.RadioButton(i).ForeColor = Color.Yellow
            End If
        Next

I know that code is wrong but you will understand what I want to do.
Thanks
 
Like most things, there is more than one way to do this. Here is one option which loops over the controls on the form and when a RadioButton is found, logic is applied to change the fore color (or whatever else you might want to do).

Dim radioButton As RadioButton

For Each item As Control In Me.Controls
    radioButton = TryCast(item, RadioButton)

    If radioButton IsNot Nothing Then
        If radioButton.Checked Then
            radioButton.ForeColor = Color.Red
        Else
            radioButton.ForeColor = Color.Green
        End If
    End If
Next item


If you have all of the RadioButtons in some other sort of container, such as a GroupBox, you could refine this to only loop over the controls within the GroupBox (or other container).

Good luck!
 
Like most things, there is more than one way to do this. Here is one option which loops over the controls on the form and when a RadioButton is found, logic is applied to change the fore color (or whatever else you might want to do).

Dim radioButton As RadioButton

For Each item As Control In Me.Controls
    radioButton = TryCast(item, RadioButton)

    If radioButton IsNot Nothing Then
        If radioButton.Checked Then
            radioButton.ForeColor = Color.Red
        Else
            radioButton.ForeColor = Color.Green
        End If
    End If
Next item


If you have all of the RadioButtons in some other sort of container, such as a GroupBox, you could refine this to only loop over the controls within the GroupBox (or other container).

Good luck!


Yes, my radiobuttons are into few diferent groupboxes. Which event I need to use into groupbox to apply this code?
 
You could theoretically execute this code as a result of any event. I tested this using a simple button on a form and its click event. If you want this to execute upon any RadioButton having its checked state changed, you could put this code into a single method that handles each of the RadioButton.CheckedChanged events. As an example, you could do something like this:

Private Sub RadioButtonCheckedChanged(sender As System.Object, e As System.EventArgs) Handles RadioButton1.CheckedChanged, RadioButton2.CheckedChanged, RadioButton3.CheckedChanged
 
You could theoretically execute this code as a result of any event. I tested this using a simple button on a form and its click event. If you want this to execute upon any RadioButton having its checked state changed, you could put this code into a single method that handles each of the RadioButton.CheckedChanged events. As an example, you could do something like this:

Private Sub RadioButtonCheckedChanged(sender As System.Object, e As System.EventArgs) Handles RadioButton1.CheckedChanged, RadioButton2.CheckedChanged, RadioButton3.CheckedChanged

Yes,but I need simple solution,something like this,but it's not working.

VB.NET:
Private Sub GetControls()
        For Each GroupBoxCntrol As Control In Me.Controls
            If TypeOf GroupBoxCntrol Is GroupBox Then
                For Each cntrl As Control In GroupBoxCntrol.Controls
                    If cntrl.GetType Is GetType(RadioButton) Then
                        If DirectCast(cntrl, RadioButton).Checked Then
                            cntrl.ForeColor = Color.Blue
                        Else
                            cntrl.ForeColor = Color.Yellow
                        End If
                    End If

                Next
            End If

        Next
    End Sub
 
What do you expect to trigger this code? Do you have this hooked up and you're saying that you're getting an error in your code block or have you not hooked it up? If you've not hooked it up, for testing purposes, you could put a button on the form and in its click event, call your method. If you want this to execute whenever any RadioButton is checked, you could modify your Private Sub GetControls() to look more like the example I gave that handles the CheckedChanged event of all your RadioButtons.
 
What do you expect to trigger this code? Do you have this hooked up and you're saying that you're getting an error in your code block or have you not hooked it up? If you've not hooked it up, for testing purposes, you could put a button on the form and in its click event, call your method. If you want this to execute whenever any RadioButton is checked, you could modify your Private Sub GetControls() to look more like the example I gave that handles the CheckedChanged event of all your RadioButtons.

Code above I call when form shows. I found some other code which works but only when form shows, I only need to loop everytime over controls.
VB.NET:
Try
            For Each pb As RadioButton In GroupBox1.Controls

                If pb.Checked = True Then
                    pb.ForeColor = Color.Blue
                Else
                    pb.ForeColor = Color.Yellow
                End If
            Next pb

        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
 
I resolve problem by combining with timer:

VB.NET:
  Private Sub GetControls()
        For Each GroupBoxCntrol As Control In Me.Controls
            If TypeOf GroupBoxCntrol Is GroupBox Then
                For Each cntrl As Control In GroupBoxCntrol.Controls
                    If cntrl.GetType Is GetType(RadioButton) Then
                        If DirectCast(cntrl, RadioButton).Checked Then
                            cntrl.ForeColor = Color.Blue
                        Else
                            cntrl.ForeColor = Color.Yellow
                        End If
                    End If
                Next
            End If
        Next
    End Sub

VB.NET:
    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Dim message As String = "I resolve a problem :)  "

        If Label1.Text = message Then
            Label1.Text = ""
        Else
            Label1.Text = Mid(message, 1, Len(Label1.Text) + 1)
        End If

    End Sub
And set timer to enabled of course.
 
Have you considered using CheckedChanged event instead of polling with a Timer to see if Checked has changed?
 
I agree with JohnH. I'd first write an event handler:
    Private Sub SetsColorUpon_CheckedChanged(sender As System.Object, e As System.EventArgs)
        Dim HandledRadioButton = TryCast(sender, RadioButton)
        If HandledRadioButton IsNot Nothing Then
            HandledRadioButton.ForeColor = IIf(HandledRadioButton.Checked, Color.Red, Color.Yellow)
        End If
    End Sub

Then I'd write code in Form.Load to recursively collect Form's RadioButtons, add them that handler, and run it once (so that they get the desired color before user has to click on them):
    Private Sub MyBase_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        AddsHandlerToRadioButtons(Me)
    End Sub
    Private Sub AddsHandlerToRadioButtons(my_form As Form)
        Dim lc As New List(Of Control)
        AddsChildrenControlsToList(my_form, lc)
        For Each c In lc
            Dim rb = TryCast(c, RadioButton)
            If rb IsNot Nothing Then
                AddHandler rb.CheckedChanged, AddressOf SetsColorUpon_CheckedChanged
                SetsColorUpon_CheckedChanged(rb, System.EventArgs.Empty)
            End If
        Next
    End Sub
    Private Sub AddsChildrenControlsToList(control_container As Control, target_list As List(Of Control))
        For Each c In control_container.Controls
            target_list.Add(c)
            AddsChildrenControlsToList(c, target_list)
        Next
    End Sub

Good Luck!
 
I agree with JohnH. I'd first write an event handler:
    Private Sub SetsColorUpon_CheckedChanged(sender As System.Object, e As System.EventArgs)
        Dim HandledRadioButton = TryCast(sender, RadioButton)
        If HandledRadioButton IsNot Nothing Then
            HandledRadioButton.ForeColor = IIf(HandledRadioButton.Checked, Color.Red, Color.Yellow)
        End If
    End Sub

Then I'd write code in Form.Load to recursively collect Form's RadioButtons, add them that handler, and run it once (so that they get the desired color before user has to click on them):
    Private Sub MyBase_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        AddsHandlerToRadioButtons(Me)
    End Sub
    Private Sub AddsHandlerToRadioButtons(my_form As Form)
        Dim lc As New List(Of Control)
        AddsChildrenControlsToList(my_form, lc)
        For Each c In lc
            Dim rb = TryCast(c, RadioButton)
            If rb IsNot Nothing Then
                AddHandler rb.CheckedChanged, AddressOf SetsColorUpon_CheckedChanged
                SetsColorUpon_CheckedChanged(rb, System.EventArgs.Empty)
            End If
        Next
    End Sub
    Private Sub AddsChildrenControlsToList(control_container As Control, target_list As List(Of Control))
        For Each c In control_container.Controls
            target_list.Add(c)
            AddsChildrenControlsToList(c, target_list)
        Next
    End Sub

Good Luck!

Thank you my friends. Special thanks to you VBobCat. This works perfectly and I agree that this is more efficient way to make same result like I made using timer. From this moment I will chose your metod everytime when event handler is necessary to change some form controls properties. Thank you again!
 
Back
Top