What's New
Q & A
Tip Jar
C# Helper...
Follow VBHelper on Twitter
MSDN Visual Basic Community
TitleBuild a big survey form in a scrolling panel at run time in Visual Basic 2005
DescriptionThis example shows how to build a big survey form in a scrolling panel at run time in Visual Basic 2005.
Keywordssurvey, scroll, scrolling panel, Visual Basic 2005, VB 2005, VB.NET
CategoriesVB.NET, Controls
This example shows how to build a large group of controls contained inside specific containers. When it starts, the program builds a series of labels to hold survey questions. For each, it makes a Panel holding four RadioButtons that the user can select to answer the questions. It puts the RadioButtons inside Panels so each group of four is in its own RadioButton group. That allows the user to select one RadioButton in each group.

When the program starts, the form's Load event handler saves the controls built at design time in the class-level arrays m_lblQuestion, m_panQuestion, and m_radChoice.

Next the code enters a loop to create the other controls it needs. For each new question, it makes a question Label and a Panel positions below the previous ones, and saves references to them in the appropriate arrays.

It adds both of these controls to the form'spanSurvey Panel control. That control contains all of the questions and answer RadioButtons. has its AutoScroll property set to True so it displays scroll bars when necessary.

Next the code enters a second loop that creates the RadioButtons for the new question. It adds the RadioButtons to the new Panel control so they form a new RadioButton group.

Public Class Form1
Private Const NUM_QUESTIONS As Integer = 20
Private Const NUM_ANSWERS As Integer = 4
Private m_lblQuestion(NUM_QUESTIONS - 1) As Label
Private m_panQuestion(NUM_QUESTIONS - 1) As Panel
Private m_radChoice(NUM_QUESTIONS - 1, NUM_ANSWERS - 1) As _

' Build controls.
' A more realistic program would load the questions and
' answers from
' a database, text file, XML file, or something else.
Private Sub Form1_Load(ByVal sender As System.Object, ByVal _
    e As System.EventArgs) Handles MyBase.Load
    m_lblQuestion(0) = Me.lblQuestion1
    m_panQuestion(0) = Me.panQuestion1
    m_radChoice(0, 0) = Me.radChoice0_1
    m_radChoice(0, 1) = Me.radChoice0_2
    m_radChoice(0, 2) = Me.radChoice0_3
    m_radChoice(0, 3) = Me.radChoice0_4

    Dim tab_index As Integer = Me.radChoice0_4.TabIndex
    For i As Integer = 1 To NUM_QUESTIONS - 1
        Dim y As Integer = m_panQuestion(i - 1).Top + _
            m_panQuestion(i - 1).Height + 3

        ' Make the question Label.
        m_lblQuestion(i) = New Label
        m_lblQuestion(i).Text = (i + 1).ToString() & ". " & _
        m_lblQuestion(i).Location = New _
            Point(m_lblQuestion(i - 1).Left, y)
        m_lblQuestion(i).TabIndex = tab_index
        tab_index += 1

        ' Make the answer Panel.
        m_panQuestion(i) = New Panel
        m_panQuestion(i).Size = m_panQuestion(i - 1).Size
        m_panQuestion(i).Location = New _
            Point(m_panQuestion(i - 1).Left, y)

        ' Make the answer RadioButtons.
        For j As Integer = 0 To NUM_ANSWERS - 1
            m_radChoice(i, j) = New RadioButton
            m_radChoice(i, j).Text = "Choice " & (j + _
            m_panQuestion(i).Controls.Add(m_radChoice(i, j))
            m_radChoice(i, j).Location = m_radChoice(i - 1, _
            m_radChoice(i, j).Name = "radChoice" & _
                i.ToString() & "_" & j.ToString()
            m_radChoice(i, j).AutoSize = True
            m_radChoice(i, j).TabIndex = tab_index
            m_radChoice(i, j).TabStop = True
            tab_index += 1
        Next j
    Next i
End Sub
The form's Reset button uses the following code to uncheck all of the form's RadioButtons. It loops through the m_radChoice array, setting each control's Checked property to False.
' Uncheck all RadioButtons.
Private Sub btnReset_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles btnReset.Click
    For Each rad As RadioButton In m_radChoice
        rad.Checked = False
    Next rad
End Sub
The program's Submit button executes the following code. It loops through each question and verifies that one of the question's RadioButtons is checked. If none is checked, the program displays a message box and sets focus to the question's first RadioButton.
' Verify that the user has selected an answer for each
' question.
Private Sub btnSubmit_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles btnSubmit.Click
    For i As Integer = 0 To NUM_QUESTIONS - 1
        Dim no_answer As Boolean = True
        For j As Integer = 0 To NUM_ANSWERS - 1
            If m_radChoice(i, j).Checked Then
                no_answer = False
                Exit For
            End If
        Next j

        ' If no answer was selected for this quesiton,
        ' say so and set focus to its first choice.
        If no_answer Then
            MessageBox.Show("Please select an answer for " & _
                "question " & (i + 1).ToString())
            m_radChoice(i, 0).Focus()
            Exit For
        End If
    Next i
End Sub
Copyright © 1997-2006 Rocky Mountain Computer Consulting, Inc.   All rights reserved.