Visual Basic 2005 for Programmers (2nd Edition)

13.6. GroupBoxes and Panels

GroupBoxes and Panels arrange controls on a GUI. GroupBoxes and Panels are typically used to group several controls of similar functionality or several controls that are related in a GUI. All of the controls in a GroupBox or Panel move together when the GroupBox or Panel is moved.

The primary difference between these two controls is that GroupBoxes can display a caption (i.e., text) and do not include scrollbars, whereas Panels can include scrollbars and do not include a caption. GroupBoxes have thin borders by default; Panels can be set so that they also have borders by changing their BorderStyle property. Figures 13.2113.22 list the common properties of GroupBoxes and Panels, respectively.

Figure 13.21. GroupBox properties.

GroupBox properties

Description

Controls

The set of controls that the GroupBox contains.

Text

Specifies the caption text displayed at the top of the GroupBox.

Figure 13.22. Panel properties.

Panel properties

Description

AutoScroll

Indicates whether scrollbars appear when the Panel is too small to display all of its controls. The default value is False.

BorderStyle

Sets the border of the Panel. The default value is None; other options are Fixed3D and FixedSingle.

Controls

The set of controls that the Panel contains.

Look-and-Feel Observation 13.4

Panels and GroupBoxes can contain other Panels and GroupBoxes for more complex layouts.

Look-and-Feel Observation 13.5

You can organize a GUI by anchoring and docking controls inside a GroupBox or Panel. The GroupBox or Panel can then be anchored or docked inside a Form. This divides controls into functional "groups" that can be arranged easily.

To create a GroupBox, drag its icon from the Toolbox onto the Form. Then drag new controls from the Toolbox into the GroupBox. These controls are added to the GroupBox's Controls property and become part of the GroupBox. The GroupBox's Text property specifies the caption.

To create a Panel, drag its icon from the Toolbox onto the Form. You can then add controls directly to the Panel by dragging them from the Toolbox onto the Panel. To enable the scrollbars, set the Panel's AutoScroll property to true. If the Panel is resized and cannot display all of its controls, scrollbars appear (Fig. 13.23). The scrollbars can be used to view all the controls in the Panelboth at design time and at execution time. In Fig. 13.23, we set the Panel's BorderStyle property to FixedSingle so that you can see the Panel in the Form.

Figure 13.23. Creating a Panel with scrollbars.

The program in Fig. 13.24 uses a GroupBox and a Panel to arrange Buttons. When these Buttons are clicked, their event handlers change the text on a Label.

Figure 13.24. Using GroupBoxes and Panels to arrange Buttons.

1 ' Fig. 13.24: FrmGroupboxPanelExample.vb 2 ' Using GroupBoxes and Panels to hold Buttons. 3 Public Class FrmGroupBoxPanelExample 4 ' event handler for btnHi's Click event 5 Private Sub btnHi_Click(ByVal sender As System.Object, _ 6 ByVal e As System.EventArgs) Handles btnHi.Click 7 lblMessage.Text = "Hi pressed" ' change text in Label 8 End Sub ' btnHi_Click 9 10 ' event handler for btnBye's Click event 11 Private Sub btnBye_Click(ByVal sender As System.Object, _ 12 ByVal e As System.EventArgs) Handles btnBye.Click 13 lblMessage.Text = "Bye pressed" ' change text in Label 14 End Sub ' btnBye_Click 15 16 ' event handler for btnLeft's Click event 17 Private Sub btnLeft_Click(ByVal sender As System.Object, _ 18 ByVal e As System.EventArgs) Handles btnLeft.Click 19 lblMessage.Text = "Far left pressed" ' change text in Label 20 End Sub ' btnLeft_Click 21 22 ' event handler for btnRIght's Click event 23 Private Sub btnRight_Click(ByVal sender As System.Object, _ 24 ByVal e As System.EventArgs) Handles btnRight.Click 25 lblMessage.Text = "Far right pressed" ' change text in Label 26 End Sub ' btnRight_Click 27 End Class ' FrmGroupBoxPanelExample

The GroupBox (named grpMain) has two ButtonsbtnHi (which displays the text Hi) and btnBye (which displays the text Bye). The Panel (named pnlMain) also has two Buttons, btnLeft (which displays the text Far Left) and btnRight (which displays the text Far Right). The pnlMain has its AutoScroll property set to true, allowing scrollbars to appear when the contents of the Panel require more space than the Panel's visible area. The Label (named lblMessage) is initially blank. To add controls to grpMain or pblMain, Visual Studio calls method Add of each container's Controls property. This code is placed in the partial class located in the file FrmGroupBoxPanelExample.Designer.vb.

The event handlers for the four Buttons are located in lines 526. We added a line in each event handler (lines 7, 13, 19 and 25) to change the text of lblMessage to indicate which Button the user pressed.

Категории