How do I associate a radio button to a group box when they are both created at runtime?

I have a recordset i'm referencing for questions and responses; some of the questions are radio buttons that I want to put within their own group box at runtime.  This way I will be able to create multiple questions with radio button answers.

If I don't associate the radio buttons with a group box, answering any radio button question makes that question's response true, all other questions become unsanswered.

Below is the code that i'm using; it's adding the controls and just performs a bringtofront on the radio buttons so they fall within the group box...but it doesn't associate the control to the group box.


If dr.Item("qType") = "rbtn" And i = 0 Then
                Dim gbox = New GroupBox
                Me.Controls.Add(gbox)
                gbox.Location = New Point(25, offset)
                gbox.Text = dr.Item("qID")
                gbox.Size = New Size(300, 90)
                offset = offset + 20
            End If
            If dr.Item("qType") = "rbtn" Then
                i = i + 1
                Dim rbtn = New RadioButton()
                Me.Controls.Add(rbtn)
                rbtn.Location = New Point(30, offset)
                rbtn.Text = dr.Item("r")
                rbtn.Checked = True
                rbtn.Size = New Size(100, 20)
                rbtn.BringToFront()
                If i = 3 Then offset = offset + 40
                If i <> 3 Then offset = offset + 20
            End If

Open in new window

LVL 1
vbNewbie2009Asked:
Who is Participating?
 
käµfm³d 👽Commented:
On a second look, I might even restructure you code a bit. (I removed the BringToFront() call since it really isn't necessary.)
If dr.Item("qType") = "rbtn" Then
    Dim gbox As GroupBox

    If i = 0 Then
        gbox = New GroupBox
        Me.Controls.Add(gbox)
        gbox.Location = New Point(25, offset)
        gbox.Text = dr.Item("qID")
        gbox.Size = New Size(300, 90)
        offset = offset + 20
    ElseIf gbox IsNot Nothing Then
        i = i + 1
        Dim rbtn = New RadioButton()
        gbox.Controls.Add(rbtn)
        rbtn.Location = New Point(30, offset)
        rbtn.Text = dr.Item("r")
        rbtn.Checked = True
        rbtn.Size = New Size(100, 20)
        If i = 3 Then offset = offset + 40
        If i <> 3 Then offset = offset + 20
    End If    
End If

Open in new window

0
 
käµfm³d 👽Commented:
You need to add the new RadioButton to the GroupBox's Controls collection, not the Form's. From the code you posted, you are going to need to widen the scope of gbox so that it can be seen in the subsequent "if" statements. I don't know what the rest of your code looks like, obviously, so going off of what you posted:
Dim gbox As GroupBox

If dr.Item("qType") = "rbtn" And i = 0 Then
    gbox = New GroupBox
    Me.Controls.Add(gbox)
    gbox.Location = New Point(25, offset)
    gbox.Text = dr.Item("qID")
    gbox.Size = New Size(300, 90)
    offset = offset + 20
End If
If dr.Item("qType") = "rbtn" AndAlso gbox IsNot Nothing Then
    i = i + 1
    Dim rbtn = New RadioButton()
    gbox.Controls.Add(rbtn)
    rbtn.Location = New Point(30, offset)
    rbtn.Text = dr.Item("r")
    rbtn.Checked = True
    rbtn.Size = New Size(100, 20)
    rbtn.BringToFront()
    If i = 3 Then offset = offset + 40
    If i <> 3 Then offset = offset + 20
End If

Open in new window

0
 
vbNewbie2009Author Commented:
Thank you so much for the feedback!   Here's the final version I ended up using.
If dr.Item("qType") = "rbtn" Then
                Dim gbox As GroupBox

                If i = 0 Then
                    gbox = New GroupBox
                    Me.Controls.Add(gbox)
                    gbox.Location = New Point(25, offset)
                    gbox.Text = dr.Item("qID")
                    gbox.Size = New Size(300, 90)
                    offset = offset + 20

                    Dim rbtn = New RadioButton()
                    gbox.Controls.Add(rbtn)
                    rbtn.Location = New Point(30, gboxOffset)
                    rbtn.Text = dr.Item("r")
                    rbtn.Checked = True
                    rbtn.Size = New Size(100, 20)
                    If i = 3 Then offset = offset + 40
                    If i <> 3 Then
                        offset = offset + 20
                        gboxOffset = gboxOffset + 20
                    End If
                ElseIf gbox IsNot Nothing Then
                    Dim rbtn = New RadioButton()
                    gbox.Controls.Add(rbtn)
                    rbtn.Location = New Point(30, gboxOffset)
                    rbtn.Text = dr.Item("r")
                    rbtn.Checked = True
                    rbtn.Size = New Size(100, 20)
                    If i = 3 Then offset = offset + 40
                    If i <> 3 Then
                        offset = offset + 20
                        gboxOffset = gboxOffset + 20
                    End If
                End If
                i = i + 1
            End If

Open in new window

0
 
vbNewbie2009Author Commented:
I've never accepted my comment and award points to someone else, this doesn't look like the proper way to handle the question so I'm going to object and repost Kaufmed's response as the solution.
0
 
vbNewbie2009Author Commented:
I like the last restucture suggestion the most and went with that method.  I had to move the i = i + 1 reference into the main if clause so it wouldn't generate 3 group boxes, and added a specific rbtn creation piece in the portion creating the primary GroupBox (otherwise I wasn't getting the first response RadioButton :-).

I really appreciate the help!, Thanks again!

...PS...I was trying to accept the code i posted but award you the points but I didn't like the warning message so i cancelled and am awarding you the points through this soltuion.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.