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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.