Search code examples
vb.netformsdebuggingradio-buttongroupbox

Making RadionButtons appear infront of a GroupBox


When I dynamically create a GroupBox and add 4 RadionButtons inside the GroupBox it somehow puts the RadionButtons behind the GroupBox.

I have the code for the GroupBox first therefore why do the RadionButtons not show over the GroupBox?

Code for reference:

multichoicegroupbox(Qnum) = New GroupBox : multichoicegroupbox(Qnum).Location = New Point(X, (Y - 5))
multichoicegroupbox(Qnum).Width = 230 : multichoicegroupbox(Qnum).Height = 120
frmQuizForStudents.Controls.Add(multichoicegroupbox(Qnum))

For MultichoiceCheckNum = 1 to 4
    rdbmultichoice(MultichoiceCheckNum) = New RadioButton
    rdbmultichoice(MultichoiceCheckNum).Location = New Point(multichoicegroupbox(Qnum).Location.X + 10,
                                                            (multichoicegroupbox(Qnum).Location.Y + (MultichoiceCheckNum * 24)))
    rdbmultichoice(MultichoiceCheckNum).Font = New Font("Arial", 9)
    rdbmultichoice(MultichoiceCheckNum).Text = multichoice(MultichoiceCheckNum)
    multichoicegroupbox(Qnum).Controls.Add(rdbmultichoice(MultichoiceCheckNum))

    Y += 24
Next MultichoiceCheckNum

When I comment out the New Point line of code for the GroupBox, the GroupBox appears in the top left of the screen with all 4 RadionButtons working on top of it.


Solution

  • Because the Location of the RadioButton is relative to their container. Instead you specify a Location relative to the Form. The buttons are there but are out of sight.

    In other words you shouldn't add the location of the GroupBox in your calculation but just consider the top/left position of the GroupBox as the position 0,0 of the coordinates for your RadioButtons.

    For MultichoiceCheckNum = 1 to 4
        rdbmultichoice(MultichoiceCheckNum) = New RadioButton
        rdbmultichoice(MultichoiceCheckNum).Location = New Point(10,
                                               10 + (MultichoiceCheckNum * 24))
        rdbmultichoice(MultichoiceCheckNum).Font = New Font("Arial", 9)
        rdbmultichoice(MultichoiceCheckNum).Text = multichoice(MultichoiceCheckNum)
        multichoicegroupbox(Qnum).Controls.Add(rdbmultichoice(MultichoiceCheckNum))
    
    Next MultichoiceCheckNum