locked
Detecting change in state of radio buttons RRS feed

  • Question

  • Using Visual Basic Express 2008

    Is there an event which can be detected when any change occurs in a group of radio buttons? My form has a group of four radio buttons, so it's more complicated than with two buttons.

    Depending on which of a group of four radio buttons is selected various other controls on the form will change their behaviour. I can raise a CheckedChanged event for each radio button separately but wondered if there was an all encompassing event which would detect any change within the group.

    Grateful for advice.

    Wednesday, May 8, 2013 12:48 PM

Answers

  • You can have a single event handler handle the CheckChanged event for all four RadioButtons.

    For example

    Private Sub radioButton_CheckedChanged(sender As Object, e As EventArgs) _
            Handles RadioButton1.CheckChanged, RadioButton2.CheckChanged, RadioButton3.CheckChanged, RadioButton4.CheckChanged
        Dim theButton As RadioButton = CType(sender, RadioButton) 'theButton is the RadioButton that was changed
        'Do something with theButton
    End Sub


    • Edited by Blackwood Wednesday, May 8, 2013 1:02 PM Add example
    • Proposed as answer by IronRazerz Wednesday, May 8, 2013 1:07 PM
    • Marked as answer by Anonymous126 Wednesday, May 8, 2013 2:49 PM
    Wednesday, May 8, 2013 12:57 PM

All replies

  • You can have a single event handler handle the CheckChanged event for all four RadioButtons.

    For example

    Private Sub radioButton_CheckedChanged(sender As Object, e As EventArgs) _
            Handles RadioButton1.CheckChanged, RadioButton2.CheckChanged, RadioButton3.CheckChanged, RadioButton4.CheckChanged
        Dim theButton As RadioButton = CType(sender, RadioButton) 'theButton is the RadioButton that was changed
        'Do something with theButton
    End Sub


    • Edited by Blackwood Wednesday, May 8, 2013 1:02 PM Add example
    • Proposed as answer by IronRazerz Wednesday, May 8, 2013 1:07 PM
    • Marked as answer by Anonymous126 Wednesday, May 8, 2013 2:49 PM
    Wednesday, May 8, 2013 12:57 PM
  • Hello,

    The following is done using a List(Of T) while the same thing can be done with a simple class, List or DataTable. The original container was created by a Duncan Mackenzie, I tweaked it back in (not sure when, it has been a while) VS2005 then as time progressed moved it to VS2008 then VS2010.

    Any ways the beauty is it is very flexible, the downside is, it can be a little effort to position.

    The screenshot below shows a custom container in the upper-right hand corner of the form while the lower left hand-corner of the form shows the selected item in the container when the user changes a selection. The project is available here.

    The container is a custom GroupBox with Radiobuttons populated in this case via a List(Of String).

    Detecting change

    Private Sub RadioButtonList1_SelectedValueChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles RadioButtonList1.SelectedValueChanged
        If RadioButtonList1.SelectedIndex > -1 Then
            lblStatusOfValueMember.Text = String.Format("Status is: {0}", RadioButtonList1.SelectedValue.ToString)
        End If
    End Sub

    Setting up the container and the data source

    With RadioButtonList1
        .AutoSize = False
        .Height = 100
        .Width = 200
        .Top = DataGridView1.Top
        .Left = (DataGridView1.Left + DataGridView1.Width) + 5
        .DataSource =
            (
                From T In [Enum].GetNames(GetType(MaritalStatus))
                Select New StatusItem With
                       {
                           .DisplayName = String.Join(" ", T.BreakUpper),
                           .Value = T
                       }
                   ).ToList
        .DisplayMember = "DisplayName"
        .ValueMember = "Value"
    End With

    Data source

    Public Enum MaritalStatus
        Married
        NotMarried
        LivesAlong
    End Enum

    While the underlying data is a List(Of Person) for the DataGridView is Person. A BindingSource Component is the master data source.

    Public Class Person
        Public Property FirstName As String
        Public Property LastName As String
        Public Property City As String
        Public Property State As String
        Public Property Status As MaritalStatus
        Public Overrides Function ToString() As String
            Return FirstName & "," & LastName & "," & City & "," & State & "," & Status.ToString
        End Function
    End Class


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem.


    Wednesday, May 8, 2013 1:28 PM
  • Thanks for this, it does exactly what I needed. I had no idea you could be versatile with these event handlers. I had assumed since you can generate them automatically you had to use all the parameters as they appeared.

    regards

    Wednesday, May 8, 2013 2:53 PM