13.6. GroupBoxes and PanelsGroupBoxes 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.
Look-and-Feel Observation 13.4
Look-and-Feel Observation 13.5
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.
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. |