GroupBox
es and Panel
sGroupBoxes
and Panel
s arrange controls on a GUI. GroupBox
es and Panel
s 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. Furthermore, a GroupBox
es and Panel
s also can be used to show or hide a set of controls at once. When you modify a container’s Visible
property, it toggles the visibility of all the controls within it.
The primary difference between these two controls is that GroupBox
es can display a caption (i.e., text) and do not include scrollbars, whereas Panel
s can include scrollbars and do not include a caption. GroupBox
es have thin borders by default; Panel
s can be set so that they also have borders by changing their BorderStyle
property. Figures 14.21–14.22 list the common properties of GroupBox
es and Panel
s, respectively.
Panel
s and GroupBox
es can contain other Panel
s and GroupBox
es for more complex layouts.
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 . |
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. |
You can organize a GUI by anchoring and docking controls inside a GroupBox
or Panel
. The GroupBox
or Panel
then can 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 a 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 at the top of the GroupBox
.
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. 14.23). The scrollbars can be used to view all the controls in the Panel
—at design time and at execution time. In Fig. 14.23, we set the Panel
’s BorderStyle
property to FixedSingle
so that you can see the Panel
in the Form
.
The program in Fig. 14.24 uses a GroupBox
and a Panel
to arrange Button
s. When these Button
s are clicked, their event handlers change the text on a Label
.
The mainGroupBox
has two Button
s—hiButton
(which displays the text Hi) and bye-Button
(which displays the text Bye). The Panel
(named mainPanel
) also has two Button
s, leftButton
(which displays the text Far Left) and rightButton
(which displays the text Far Right). The mainPanel
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 messageLabel
) is initially blank. To add controls to mainGroupBox
or main-Panel
, Visual Studio calls method Add
of each container’s Controls
property. This code is placed in the partial class located in the file GroupBoxPanelExampleForm.Designer.cs
.
The event handlers for the four Button
s are located in lines 18–39. Lines 20, 26, 32 and 38 change the text of messageLabel
to indicate which Button
the user pressed.