Trung Nguyen
Trung Nguyen

Reputation: 25

RadioButton does not show up when inside a panel within a panel

This is my first question ever, so please spare me if I did something wrong.

I have a small survey form that automatically draw questions and answers from a SQL server table, and create a question label (Label_Questionnaire(i)), a panel to nest all radiobuttons for the answer of each question (Panel_Response(i)), and 3 radiobuttons(yes, no, n/a), named RadioButton_Answers(i)_1 . All questions and answers are inside a big panel (Panel_Survey) to allow user to scroll up and down (around 50 questions).

When I run the program, I can only see questions but none of the radiobuttons are showing. What I did try are:

How do I make those radiobuttons show up? If not, are there any better designs for this kind of survey form? Thank you for any advice, in advance!

Below is my code:

Protected Overrides Sub OnLoad(e As EventArgs)
    Dim PanelCount As Integer
    Dim QuestionName As String
    Dim Response1 As String
    Dim Response2 As String
    Dim Response3 As String
    Dim InitialX As Integer = Panel_Survey.Left
    Dim InitialY As Integer = Panel_Survey.Top
    Dim SizeX As Integer = 1000
    Dim SizeY As Integer = 25

    'Load the survey

    Try
        'Get a list of questions and answers into array of list

        Dim ListofQuestionandAnswers As New List(Of List(Of String))
        Dim conn As New SqlClient.SqlConnection
        conn.ConnectionString = ConnectionString
        Dim CommandString As String = "SELECT [QuestionID], [QuestionName] ,[Response1],[Response2],[Response3] FROM [Question_List] ORDER BY [QuestionID]"
        Dim Command As New SqlClient.SqlCommand
        Command.CommandText = CommandString
        Command.Connection = conn
        Dim dr As SqlClient.SqlDataReader
        conn.Open()
        dr = Command.ExecuteReader
        While dr.Read
            Dim ls As New List(Of String)
            ls.Add(dr.GetValue(0).ToString)
            ls.Add(dr.GetValue(1).ToString)
            ls.Add(dr.GetValue(2).ToString)
            ls.Add(dr.GetValue(3).ToString)
            ls.Add(dr.GetValue(4).ToString)
            ListofQuestionandAnswers.Add(ls)
        End While
        conn.Close()

        PanelCount = ListofQuestionandAnswers.Count

        For i = 0 To ListofQuestionandAnswers.Count - 1

            QuestionName = ListofQuestionandAnswers(i)(1)
            Response1 = ListofQuestionandAnswers(i)(2)
            Response2 = ListofQuestionandAnswers(i)(3)
            Response3 = ListofQuestionandAnswers(i)(4)

            Dim Label_Questionnaire As New Label
            Dim Panel_Response As New Panel
            Dim RadioButton_Answers_1 As New RadioButton
            Dim RadioButton_Answers_2 As New RadioButton
            Dim RadioButton_Answers_3 As New RadioButton



            'Condition the label 

            With Label_Questionnaire
                .Parent = Panel_Survey
                .Name = "Label_Questionnaire" + i.ToString
                .Font = New Font("Calibri", 11, FontStyle.Regular)
                .Text = QuestionName
                .ForeColor = Color.Black
                .Location = New Point(InitialX, InitialY)
                .AutoSize = True

            End With

            'Condition the panel

            With Panel_Response
                'Panel_Survey.Controls.Add(Panel_Response)
                .Parent = Panel_Survey
                .Name = "Panel_Questionnaire" + i.ToString
                .Location = New Point(InitialX + 880, InitialY)
                .Width = 250
                .Height = 25
                .BringToFront()
            End With

            Dim j As Integer
            Dim h As Integer
            j = Panel_Response.Left
            h = Panel_Response.Top



            'Condition the radiobuttons for answers

            With RadioButton_Answers_1
                .Parent = Panel_Response
                .Name = "RadioButton_Answers" + i.ToString + "_1"
                .Font = New Font("Calibri", 11, FontStyle.Regular)
                .Text = Response1
                .ForeColor = Color.Black
                .Location = New Point(j, h)
                .AutoSize = True


                h += RadioButton_Answers_1.Height
            End With

            With RadioButton_Answers_2
                .Parent = Panel_Response
                .Name = "RadioButton_Answers" + i.ToString + "_2"
                .Font = New Font("Calibri", 11, FontStyle.Regular)
                .Text = Response2
                .ForeColor = Color.Black
                .Location = New Point(RadioButton_Answers_1.Right, h)
                .AutoSize = True
            End With



            With RadioButton_Answers_3
                .Parent = Panel_Response
                .Name = "RadioButton_Answers" + i.ToString + "_3"
                .Font = New Font("Calibri", 11, FontStyle.Regular)
                .Text = Response3
                .ForeColor = Color.Black
                .Location = New Point(RadioButton_Answers_2.Right, h)
                .AutoSize = True
            End With

            InitialY = InitialY + SizeY + 10

        Next

    Catch ex As Exception
        MessageBox.Show(String.Format("Error: {0}", ex.Message), "Error while creating questions and answers", MessageBoxButtons.OK, MessageBoxIcon.Error)
    End Try
End Sub

Upvotes: 1

Views: 126

Answers (1)

Caius Jard
Caius Jard

Reputation: 74660

Few basic problems here:

  • You seem to assume that when a control is placed inside a panel, it must be placed at a location relative to the form. It doesn't; it places relative to the Panel which has its own coordinate system starting at 0,0 in the top left of the panel - you initialize j and h (for locations of the radio buttons) to the Left and Top of the panel they're in, but they should be inited to 0,0 if you want the radiobuttons to start at the top left of the panel. If the Panel is placed at 300,300 on a form, and you place a radiobutton inside the panel also at 300,300 (because you copied its left and top) then the radio button will look like it's at 600,600 on the form because its at 300,300 inside a panel that is at 300,300

  • You only make your panel 250 wide - it's barely wide enough to show a single radio button

  • You increment h once, by an amount that means the next radiobutton disappears off the bottom of the panel (which is 25 pixels high)

Here:

With Panel_Response
  'Panel_Survey.Controls.Add(Panel_Response)
  .Parent = Panel_Survey
  .Name = "Panel_Questionnaire" + i.ToString
  .Location = New Point(InitialX + 880, InitialY)
  .Width = 2500
  .Height = 25
  .BringToFront()
  .BackColor = Color.Red
End With

Dim j As Integer
Dim h As Integer
j = 0
h = 0

Paste that over your app code and run it again. I made the panel BackColor red so you can more easily see where the panel is

I guess you need to decide how you want your UI to look. If the radiobuttons are laid out vertically, don't increment X when you add them to the panel (by setting the parent property). Make the panel tall enough to accommodate them (25 px not enough)


Use a FlowLayoutPaanel or TableLayoutPanel instead

Upvotes: 2

Related Questions