Search code examples
vb.netvisual-studioradio-buttondynamically-generated

Can't Generate Multiple Radio Buttons in VB.Net


A portion of the program I'm currently working on will involve me making multiple rows of radio buttons for each entry stored in an array. This will involve programmatically creating group boxes with three radio buttons each in them. However, I can't seem to get my code to generate more than 1 button per box. My sandbox code is below.

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Dim rb As RadioButton
    Dim gb As GroupBox

    For i As Integer = 1 To 3

        gb = New GroupBox

        With gb
            .Size = New System.Drawing.Size(400, 100)
            .Name = "gb" & i
            .Top = 30 + 100 * (i - 1)
            .Left = 30
        End With

        Me.Controls.Add(gb)

        For j As Integer = 1 To 3

            rb = New RadioButton

            With rb
                .Top = 50
                .Left = 40 * j
                .Name = "rb" & i & j
            End With

            Me.Controls("gb" & i).Controls.Add(rb)
        Next

    Next

    Form2.Show()

End Sub

Worth noting: Find/Replacing 'RadioButton' with 'TextBox' gives me the three textboxes--is there some property to Radio Buttons that I'm missing?

Thanks in advance!!


Solution

  • Once you define the width property, the checkboxes show up.

    I found that the default size at least on my machine was 104. So, moving to the right by 40 was not enough.

                With rb
                    .Top = 50
                    .Left = 40 * j
                    .Name = "rb" & i & j
                    .BackColor = System.Drawing.Color.Orange
                    '.Text = "rb" & i & j
                    .Width = 40
                End With
    

    The background color (which can be removed, helps to show where the checkbox is actually located).