Implementing control arrays in

The project I am currently working on is MMPI test module. One of the most challenging parts was to develop a user interface that will accommodate answers for over 560 questions with three options (True, False, Don't care / pass) each. This would have been done this in VB6 using three control arrays of radio buttons. But in VB.Net, control arrays are no longer supported.

Then I found out that VB.Net supports classes. I was fascinated as always with concept of OOPS, and applied it to my need. The result was fabulous. Here is the code for the class and a function which uses the code from a VB.Net form. Without the class I would have ended up drawing manually 600*3=1800 radio buttons on the form and then linking them with appropriate check changed event, which would have been a nightmare for a lazy fellow like me.

You can vary this code in any form to accommodate controls other than radiobuttons.

Code explanation:

The Code for this array is very simple.

1. I made a Groupbox control using code inside the class Questionarraysa and attached four controls to the groupbox(3 radio buttons and a label). No need to load the controls just use Add function of Controls collection of Groupbox to attach controls

2. After which I Positioned each of the controls within the Groupbox by setting their Top and Left properties.

3. Finaly I attached the Checkchanged Event to each of the radiobuttons, to the functions I wrote. Now one question template class is ready. The calls function AddNewQuestion will return a Groupbox control (which will contain the label and radiobutton controls in it).

4. In the form then, an array of the template class (Questionarray) was declared and for each element Addnewquestion was called.

5. The groupbox which was returned by the call was then attached to the form using Add function of the Controls collection.

The effect is the array shares code for the Eventhandler and we can identify each element uniquely using Array index as in VB 6.0
Public Class QuestionsArray
                          Inherits System.Collections.CollectionBase
                          Private mywindow As System.Windows.Forms.Form
                          Public TrueButton As System.Windows.Forms.RadioButton
                          Public FalseButton As System.Windows.Forms.RadioButton
                          Public DButton As System.Windows.Forms.RadioButton
                          Public AnswerGroup As System.Windows.Forms.GroupBox
                          Public QstNo As System.Windows.Forms.Label
                          Private Questionno As Integer
                          Private Answer As String
                          Sub New(ByVal Questionno1 As Integer, ByVal QSTWIND As System.Windows.Forms.Form)
                              Questionno = Questionno1
                              mywindow = QSTWIND
                              TrueButton = New System.Windows.Forms.RadioButton
                              FalseButton = New System.Windows.Forms.RadioButton
                              DButton = New System.Windows.Forms.RadioButton
                              AnswerGroup = New System.Windows.Forms.GroupBox
                              QstNo = New System.Windows.Forms.Label
                          End Sub
                          Public Function AddNewQuestion(ByVal Questionno As Integer, ByVal toppos As Integer, ByVal leftpos As Integer) As System.Windows.Forms.GroupBox
                              Dim a As EventArgs
                              Dim leftst = 10
                              Dim ctlwidth As Integer
                              Dim ctlspacing As Integer
                              ctlwidth = 50
                              ctlspacing = 60
                              QstNo.Top = 15
                              QstNo.Left = leftst
                              QstNo.Width = 0.6 * ctlwidth
                              QstNo.Text = Questionno.ToString() & "."
                              TrueButton.Top = 10
                              leftst = leftst   0.6 * ctlspacing
                              TrueButton.Left = leftst
                              TrueButton.Width = ctlwidth
                              TrueButton.Height = 24
                              TrueButton.Text = "True"
                              AddHandler TrueButton.CheckedChanged, AddressOf AnswerkeysT
                              FalseButton.Top = 10
                              leftst = leftst   ctlspacing
                              FalseButton.Left = leftst
                              FalseButton.Width = ctlwidth
                              FalseButton.Height = 24
                              FalseButton.Text = "False"
                              AddHandler FalseButton.CheckedChanged, AddressOf AnswerkeysF
                              DButton.Top = 10
                              leftst = leftst   ctlspacing
                              DButton.Left = leftst
                              DButton.Width = 2.5 * ctlwidth
                              DButton.Height = 24
                              DButton.Text = "Don't care/Pass"
                              leftst = leftst   2.7 * ctlwidth
                              AddHandler DButton.CheckedChanged, AddressOf AnswerkeysD
                              AnswerGroup.Width = leftst
                              AnswerGroup.Height = 40
                              AddNewQuestion = AnswerGroup
                          End Function
                          Private Sub AnswerkeysT(ByVal sender As Object, ByVal e As System.EventArgs)
                              Answer = "True"
                          End Sub
                          Private Sub AnswerkeysF(ByVal sender As Object, ByVal e As System.EventArgs)
                              Answer = "False"
                          End Sub
                          Private Sub AnswerkeysD(ByVal sender As Object, ByVal e As System.EventArgs)
                              Answer = "Do'nt Care"
                          End Sub
                      End Class
                      Public Class Form1
                          Inherits System.Windows.Forms.Form
                      #Region " Windows Form Designer generated code "
                          Public Sub New()
                              'This call is required by the Windows Form Designer.
                              'Add any initialization after the InitializeComponent() call
                          End Sub
                          'Form overrides dispose to clean up the component list.
                          Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
                              If disposing Then
                                  If Not (components Is Nothing) Then
                                  End If
                              End If
                          End Sub
                          'Required by the Windows Form Designer
                          Private components As System.ComponentModel.IContainer
                          'NOTE: The following procedure is required by the Windows Form Designer
                          'It can be modified using the Windows Form Designer.  
                          'Do not modify it using the code editor.
                          <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
                              Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
                              Me.AutoScroll = True
                              Me.ClientSize = New System.Drawing.Size(408, 414)
                              Me.Name = "Form1"
                              Me.Text = "Form1"
                          End Sub
                      #End Region
                          Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
                          End Sub
                          Private Sub LoadRadioButtons()
                              Dim tempgroup As System.Windows.Forms.GroupBox
                              Dim Thissession(600) As QuestionsArray
                              Dim i As Integer
                              Dim topst As Integer
                              topst = 10
                              i = 1
                              For i = 0 To 599
                                  Thissession(i) = New QuestionsArray(i   1, Me)
                                  tempgroup = Thissession(i).AddNewQuestion(i   1, topst, 10)
                                  tempgroup.Top = topst
                                  topst = topst   50
                                  tempgroup.Left = 10
                          End Sub
                      End Class

Open in new window


Comments (0)

Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.