Binding to Radio Buttons in a Group Box

I have a few radio buttons in a group box and want to bind data to them all as a group, like if value "3" is the current value in the record, select the third radio button, etc.

It's all so easy in ASP.NET with RadioButtonLists, but this Windows stuff is a little new to me. 

Does anyone have any good resources for binding to a set of radio buttons


Answer this question

Binding to Radio Buttons in a Group Box

  • Mike.Dormitorio

    I've had to build a custom control that uses the panel as it's base, then I have it to add as many radio buttons (kind of like how you do tablestyles for the datagrid) and I can set a value for each radio button. Then I set the binding to the control (that hosts the radiobuttons) and the radiobutton that is selected sets the binding to the value given.

    It works rather nicely.

  • Xavier Pillons

    I don't know that it is the best way, but I can suggest <I>a</I> way...

    When your form is created, declare a class-wide ArrayList variable and then add each RadioButton to the ArrayList.
    <HR>
    Class MyForm1

    Private myRadioButtons as New ArrayList

    Private Sub Form_Load
    myRadioButtons.Add(RadioButton1)
    myRadioButtons.Add(RadioButton2)
    myRadioButtons.Add(RadioButton3)
    'etc...
    End Sub

    End Class
    <HR>

    Now you'll need to choose an event that fires whenever the user changes records.  If you've provided controls to navigate, you can use their events, but I'd suggest you just use a BindingManagerBase object.
    <HR>
    'Same as before, but with class-wide binding manager for datasource
    Class MyForm1

    Private myRadioButtons as New ArrayList
    Private WithEvents BMB as BindingManagerBase

    Private Sub Form_Load
    myRadioButtons.Add(RadioButton1)
    myRadioButtons.Add(RadioButton2)
    myRadioButtons.Add(RadioButton3)
    'etc...
    BMB = Me.BindingContext("[Same DataSource as Navigation Object]")
    End Sub

    'Add the Event Handler for the PositionChanged Event
    Private Sub BMB_PositionChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles BMB.PositionChanged
    'Set the appropriate RadioButton.Checked value based on value in column "myCol"
    myRadioButtons.Item(cInt(BMB.Current.Item("MyCol"))).Checked = True

    End Sub

    End Class
    <HR>

    Of couse, this assumes that there will be a valid RadioButton at the index in the ArrayList specified by the value of myCol in the current row.  I'm sure theres a better way, this is just the first that came to mind.

  • Binding to Radio Buttons in a Group Box