Search code examples
vb.netselectradio-button

How to get value from dynamic radio button


I'm creating the 3 radio buttons in a private sub like such:

For counter As Integer = 0 To rc - 1

    'controller name Radio button and properties.
    Dim dynRadio As New RadioButton()
    Me.Controls.Add(dynRadio)
    With dynRadio
       .Name = CStr(ds.Tables("MakeThisNameMeaningful").Rows(counter).Item(0))
       .Location = New Point(xAxis, yAxis)
       .TabStop = False
       .Text = CStr(ds.Tables("MakeThisNameMeaningful").Rows(counter).Item(0))
       .Width = 80
    End With

    yAxis = yAxis + 40
 Next

The radios get drawn ok. So I have 3 radios with the text (result of select from db) controller1, controller2, controller3

I've triead all sorts and couldnt' find anything on Google. Oh, I should mention that I'm trying to get the radio value from another private sub. I want to do along the lines of:

If controller1.Selected = true then
    'do stuff
End if

I know the above is wrong but not sure how to determine which radio is selected :(

Cheers, J


Solution

  • You can iterate through your radio buttons in your 2nd private sub.

    Dim radios = Controls.OfType(Of RadioButton).AsQueryable()
    
    For Each r As RadioButton In radios
        If r.Checked Then
          'this radio is checked. do something.
        End If
    Next