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 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.

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.

Look and Feel Observation 13 6

Use Panels with scrollbars to avoid cluttering a GUI and to reduce the GUI's size.

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.

(This item is displayed on page 617 in the print version)

 1 // Fig. 13.24: GroupboxPanelExampleForm.cs
 2 // Using GroupBoxes and Panels to hold Buttons.
 3 using System;
 4 using System.Windows.Forms;
 5
 6 // Form that displays a GroupBox and a Panel
 7 public partial class GroupBoxPanelExampleForm : Form
 8 {
 9 // default constructor
10 public GroupBoxPanelExampleForm()
11 {
12 InitializeComponent();
13 } // end constructor
14
15 // event handler for Hi Button
16 private void hiButton_Click( object sender, EventArgs e )
17 {
18 messageLabel.Text = "Hi pressed"; // change text in Label
19 } // end method hiButton_Click
20
21 // event handler for Bye Button
22 private void byeButton_Click( object sender, EventArgs e )
23 {
24 messageLabel.Text = "Bye pressed"; // change text in Label
25 } // end method byeButton_Click
26
27 // event handler for Far Left Button
28 private void leftButton_Click( object sender, EventArgs e )
29 {
30 messageLabel.Text = "Far left pressed"; // change text in Label
31 } // end method leftButton_Click
32
33 // event handler for Far Right Button
34 private void rightButton_Click( object sender, EventArgs e )
35 {
36 messageLabel.Text = "Far right pressed"; // change text in Label
37 } // end method rightButton_Click
38 } // end class GroupBoxPanelExampleForm
 

The GroupBox (named mainGroupBox) has two ButtonshiButton (which displays the text Hi) and byeButton (which displays the text Bye). The Panel (named mainPanel) also has two Buttons, 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 mainPanel, 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 Buttons are located in lines 1637. We added a line in each event handler (lines 18, 24, 30 and 36) to change the text of messageLabel to indicate which Button the user pressed.

Preface

Index

    Introduction to Computers, the Internet and Visual C#

    Introduction to the Visual C# 2005 Express Edition IDE

    Introduction to C# Applications

    Introduction to Classes and Objects

    Control Statements: Part 1

    Control Statements: Part 2

    Methods: A Deeper Look

    Arrays

    Classes and Objects: A Deeper Look

    Object-Oriented Programming: Inheritance

    Polymorphism, Interfaces & Operator Overloading

    Exception Handling

    Graphical User Interface Concepts: Part 1

    Graphical User Interface Concepts: Part 2

    Multithreading

    Strings, Characters and Regular Expressions

    Graphics and Multimedia

    Files and Streams

    Extensible Markup Language (XML)

    Database, SQL and ADO.NET

    ASP.NET 2.0, Web Forms and Web Controls

    Web Services

    Networking: Streams-Based Sockets and Datagrams

    Searching and Sorting

    Data Structures

    Generics

    Collections

    Appendix A. Operator Precedence Chart

    Appendix B. Number Systems

    Appendix C. Using the Visual Studio 2005 Debugger

    Appendix D. ASCII Character Set

    Appendix E. Unicode®

    Appendix F. Introduction to XHTML: Part 1

    Appendix G. Introduction to XHTML: Part 2

    Appendix H. HTML/XHTML Special Characters

    Appendix I. HTML/XHTML Colors

    Appendix J. ATM Case Study Code

    Appendix K. UML 2: Additional Diagram Types

    Appendix L. Simple Types

    Index



    Visual C# How to Program
    Visual C# 2005 How to Program (2nd Edition)
    ISBN: 0131525239
    EAN: 2147483647
    Year: 2004
    Pages: 600

    Similar book on Amazon

    Flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net