Visual Basic 2005 Guide
Working With Groups Of Controls


Sometimes we want to deal with controls as a group. There are two controls in the toolbox that can do this. The groupbox and panel can both act as containers for other controls.. There are some differences. The groupbox control has a caption and the panel box is capable of displaying scrollbars. In both cases, the MSDN will give you more detail about the use of these controls than is contained in the simple demonstration below.

Using The Panel Control

Start a new project and place a panel control on the form. Change its name to pnlDemo and set its borderstyle to FixedSingle. Make sure that the panel is selected n the form and then draw a textbox within the bounds of the panel you have on the form. Copy the textbox and paste another 3 on the panel. Do not name the textboxes.

You should have something like this,

panel form

Test that you have placed the textboxes correctly by moving the panel. If the textboxes don't move with the panel, then you need to try again.

Accessing Controls Contained In A Panel

A reference to the controls contained on the panel is available via the Controls property of the panel. The following code adds some text to each of the textboxes.

Dim intCounter As Integer
For intCounter = 0 To 3
   pnlDemo.Controls(intCounter).Text = "Hello World"

Place this code in the form's load event and run the program to test it.

The loop goes from 0 to 3 because we have exactly 4 controls in the panel. We don't have to access alll of the controls at once. Replace this code with the following single line and see what happens when you run it,

pnlDemo.Controls(0).Text = "Hello World"

Change the number in brackets and try again. Do you see what is happening? You can also access the controls by name,

pnlDemo.Controls("TextBox1").Text = "Hello World"

Matching Array Data To Panel Controls

The Controls contained in a panel are contained in a collection. Read the MSDN library to find out more about how the collection data type works. Collections are different to arrays but we can use an array to update the controls. Add a button to the form and use the following code in its click event.

Dim strPanelData() As String = {"Earl", "Is", "Name", "My"}
Dim intCounter As Integer
For intCounter = 0 To 3
   pnlDemo.Controls(intCounter).Text = strPanelData(intCounter)

Multidimensional Arrays With Panels

We can update the panel with data stored in an array with more than one dimension. The following procedure can be used to do this.

Private Sub updatePanel(ByVal intType As Integer)
   Dim strPanelData(,) As String = {{"Earl", "Is", "Name", "My"}, {"News", "O'Clock", "Nine", "The"}}
   Dim intCounter As Integer
   For intCounter = 0 To 3
      pnlDemo.Controls(intCounter).Text = strPanelData(intType, intCounter)
End Sub

Look carefully at how the array is declared and how we access it at runtime. Now add two buttons. In the click event handler for the first, write the following code,


The code for the second button should be as follows,


Reusing The Panel

Once you have set up a group of controls like this, you can copy and paste the panel (along with all o fthe controls contained) and reuse your code. At design time, click on the panel with the right mouse button and copy and paste another panel onto the form. Name this panel, pnlTwo.

Change the procedure so that it reads like this,

Private Sub updatePanel(ByVal intType As Integer, ByVal myPanel As System.Windows.Forms.Panel)
   Dim strPanelData(,) As String = {{"Earl", "Is", "Name", "My"}, {"News", "O'Clock", "Nine", "The"}}
   Dim intCounter As Integer
   For intCounter = 0 To 3
      myPanel.Controls(intCounter).Text = strPanelData(intType, intCounter)
End Sub

Change the code for the first of the buttons you made for the last part of this task. It should now read,

updatePanel(0, pnlDemo)
updatePanel(1, pnlTwo)

This code will update the first panel with the first set of words and the second panel with the other set. Monkey about with this stuff until you are happy.