Visual Basic 2008 Programming: Business Applications with a Design Perspective
Chapter 1 Chapter 2 Chapter 3 Chapter 4 Chapter 5 Chapter 6 Chapter 7 Chapter 8 Chapter 9
Chapter 10 Chapter 11 Chapter 12 Chapter 13 Chapter 14 Appendix A Appendix B Appendix C Home
Last change: February 13 2016 18:48:19.

 Chapter in PDF

Table of Contents

Chapter 3: User Interface Design: Visual Basic Controls and Events
Last change: February 13 2016 18:47:26.

The Group Box

A VB control often used to help group related data fields is the group box. As its name suggests, the group box provides a box within which a group of controls can be placed (Figure 3-5). It has a Text property that can be used to indicate its content. When used properly, the group box not only provides a means of grouping controls logically, but also facilitates an organized arrangement of the form layout.

Figure 3-5
The group box

Design Time Behavior

Controls within the group box are treated as one logical unit. When the box is moved, all controls inside the box move with it. If you delete the group box, all controls within it are also deleted. Because the group box has the capability to hold other controls, it is recognized as a type of container.

Placing Controls in a Group Box

At design time, to place a control in the group box, draw the control in the group box. You can also first draw a control onto the form and then drag it into the group box. In either case, the control becomes a logical part of the group box and will be handled accordingly. When you move the group box, the control will move with the group box.  A simplified form layout with two group boxes is presented in Figure 3-6 as an illustration.

Try This
Place a group box in the left upper corner of a new form and then double-click the TextBox icon in the Toolbox. The text box should appear in the group box. Move the group box. Notice that the text box moves with the group box, showing that the text box is a logical part of the group box.
Figuire 3-6
Group boxes

The steps to draw a control onto a form were discussed in Chapter 2. To create the interface in Figure 3-6, follow these steps:

  1. Draw one group box on a new form. Adjust the size and position of the group boxes so that it looks similar to the one on the left in the figure.
  2. Draw one label inside the group box.
  3. Copy and paste 4 additional labels in the group box. Right click on the label. Select Copy in the popup context menu. Right click in the group box and select Paste. An additional label should appear. Repeat pasting to obtain a total of 5 labels in the group box. (Alternatively, click the label and press Ctrl+C to copy. Then press Ctrl+V to paste.)
  4. Adjust the positions of these labels and set their Text properties to proper texts as shown in the figure.
  5. Draw one text box onto the group box then perform copy and paste to get a total of 5 text boxes using the same method as described in 3. Adjust their sizes and positions so that they look similar to the five text boxes in the figure.
  6. Set the Text property for the group box to Name and Address.
  7. Perform a copy and paste on the group box. You should see another group box containing the same number of labels and textboxes in the form.
  8. Place the new group box on the right of the original one. Set the Text properties for the right group box and all controls inside the right group box.
When setting various properties of several controls of the same type, work with one property at a time instead of one control at a time. As you switch from one control to another, the same property you have just worked on will be highlighted in the Properties window, making it easy to locate the property and change its setting.

As a reminder, before you start to write any code for the project, set the Name properties of all the controls to be referenced in the code.

Run Time Behavior, Visible Property, and Enabled Property

At run time, all controls contained in the group box control are treated as a single unit. For example, assuming the left group box control in Figure 3-6 is named grbAcctHolderAddr, executing the following code will make the entire group box invisible. None of the controls in the group box can be seen.

grbAcctHolderAddr.Visible = False

Similarly, the following code will disable all controls in that group box; that is, none of the text boxes in the group box can receive focus or accept any input from the user.

grbAcctHolderAddr.Enabled = False

Most of the VB controls have the Visible and Enabled properties. The default values of these properties are True. They can be set at design time or run time. At run time, a control with its Visible property set to False will be invisible as if it did not exist. After the property is changed to True, the control will appear again. You can manipulate this property with your code to hide the control when it is not needed in your program context, and to show it only when it is needed.

When a VB control, such as a text box, has the focus, the user can key in data. A control can receive focus only when it is enabled. A control with its Enabled property set to False cannot receive focus or input. Its appearance will also change to light gray to indicate its disabled status.

Last change: February 13 2016 18:47:27.