View
212
Download
0
Category
Tags:
Preview:
Citation preview
1
Chapter Ten
Using Controls
2
Objectives
• Learn about Controls• How to create a Form containing Labels• How to set a Label’s Font• How to add Color to a Form• How to add CheckBox and RadioButton
objects to a Form
3
Objectives
• How to add a PictureBox to a Form• How to add ListBox, ComboBox, and
CheckedListBox items to a Form• How to add functionality to a ListBox with one
SelectedItem• How to add functionality to a ListBox with
multiple SelectedItems• How to supply a default selection for a ListBox
4
Understanding Controls
• The Control class provides the definition for GUI objects such as Forms and Buttons
• The Control class has 23 direct descendants, some of which have their own descendants
• The Visual Studio Help documentation can be a useful resource when using Controls
5
Understanding Controls
• Control’s inheritance hierarchy
6
Understanding Controls
• A MarshalByRefObject object is one you can instantiate on a remote computer
• The Component class provides containment and cleanup for other objects
• The Control class implements very basic functionality required by classes that appear to the user
• The Control class handles user input through the keyboard and pointing device
7
Creating a Form with Labels
• A Label is one of the simplest GUI Control objects you can place on a form
• You typically use a Label control to provide descriptive text for another Control
• You can create a Label by calling the class constructor
8
Creating A Form with Labels
• FormWithLabels program and Output
9
Setting a Label’s Font
• You use the Font class to change the appearance of printed text on your Forms
• If the font size contains a decimal point, it must be of type float
• You can create a Font using FontStyles• Once you have defined a Font, you can set a Label’s
Font with a statement like label1.Font = myFont
10
Setting a Label’s Font
• Font dialog box
11
Setting a Label’s Font
• Label with new Font, FontStyle, and Size
12
Adding Color to a Form
• The Color class contains a wide variety of predefined Colors that you can use with your Controls
• Visual Studio also allows you to create custom colors• Examples of using Colors:
label1.BackColor = System.Drawing.Color.Blue;
label1.BackColor = Color.Blue;
13
Using CheckBox and RadioButton Objects
• The ButtonBase class has three direct descendants: Button, CheckBox, and RadioButton
• When a Form contains CheckBoxes, any number of them can be checked or unchecked at the same time
• RadioButtons are similar to CheckBoxes, except that when placed on a Form, only one RadioButton can be selected at a time
• Both CheckBox and RadioButton objects have a Checked property and a CheckedChanged() method
14
Adding a PictureBox to a Form• A PictureBox is a Control in which you can display graphics
• Lincoln Room Form with Image
15
Adding ListBox, CheckListBox, and ComboBox Controls to a Form
• ListBox,ComboBox, and CheckedListBox objects descend from the same family—they all are list-type widgets that descend from ListControl
• The ListBox Control enables you to display a list of items that the user can select by clicking
• With a ListBox, you can allow the user to make a single selection only or multiple selections by setting the SelectionMode property
16
Adding Functionality to a ListBox with One SelectedItem
• The SelectedItem property of a ListBox contains the value of the item a user has selected
• The easiest way to add functionality to a ListBox is by using the Visual Studio IDE
17
Adding Functionality to a ListBox with One SelectedItem
• Using the String Collection Editor to type ListBox options
18
Adding Functionality to a ListBox with One SelectedItem
• Selecting a ListBox option to change a Label
19
Adding Functionality to a ListBox with Multiple SelectedItems
• When you create a ListBox, by default its SelectionMode is One
• When a ListBox mode allows for more than one selection you use the SelectedItems array that contains a list of all currently selected item names
• You access each SelectedItems element in the same way you access any other array element
• You can determine how many items are selected by using the SelectedItems.Count field
20
Adding Functionality to a ListBox with Multiple SelectedItems
• Application that uses a ListBox that allows multiple selections
21
Supplying a Default Selection for a ListBox
• When you execute a program containing a ListBox, at first no items are selected; highlighting appears within a ListBox only after you click an option
• You can force an item to be the default by using the SetSelected() method
• The SetSelected() method requires two arguments—the position of the item to select and a Boolean value
22
Supplying a Default Selection for a ListBox
• Typical execution of Hemingway Homes application
23
Chapter Summary
• The Control class provides the definitions for GUI objects• Typically, you use a Label control to provide descriptive
text for another Control object• You use the Font class to change the appearance of
printed text on Forms• The Color class contains a wide variety of predefined
Colors that you can use with your Controls• The Button, CheckBox, and RadioButton classes all
descend from ButtonBase
24
Chapter Summary
• A PictureBox is a Control in which you can display graphics from a bitmap, icon, JPEF, GIF, or other image file type
• ListBox, ComboBox, and CheckedListBox objects descend from the same family
• The SelectedItem property of a ListBox contains the value of the item a user has selected
• When a ListBox mode allows for more than one selection, instead of a SelectedItem field, you use a SelectedItems array
• You can use the SetSelected() method to force a ListBox item to be the default
Recommended