1 Chapter Ten Using Controls. 2 Objectives Learn about Controls How to create a Form containing...

24
1 Chapter Ten Using Controls

Transcript of 1 Chapter Ten Using Controls. 2 Objectives Learn about Controls How to create a Form containing...

Page 1: 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.

1

Chapter Ten

Using Controls

Page 2: 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.

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

Page 3: 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.

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

Page 4: 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.

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

Page 5: 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.

5

Understanding Controls

• Control’s inheritance hierarchy

Page 6: 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.

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

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

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

Page 8: 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.

8

Creating A Form with Labels

• FormWithLabels program and Output

Page 9: 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.

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

Page 10: 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.

10

Setting a Label’s Font

• Font dialog box

Page 11: 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.

11

Setting a Label’s Font

• Label with new Font, FontStyle, and Size

Page 12: 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.

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;

Page 13: 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.

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

Page 14: 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.

14

Adding a PictureBox to a Form• A PictureBox is a Control in which you can display graphics

• Lincoln Room Form with Image

Page 15: 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.

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

Page 16: 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.

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

Page 17: 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.

17

Adding Functionality to a ListBox with One SelectedItem

• Using the String Collection Editor to type ListBox options

Page 18: 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.

18

Adding Functionality to a ListBox with One SelectedItem

• Selecting a ListBox option to change a Label

Page 19: 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.

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

Page 20: 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.

20

Adding Functionality to a ListBox with Multiple SelectedItems

• Application that uses a ListBox that allows multiple selections

Page 21: 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.

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

Page 22: 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.

22

Supplying a Default Selection for a ListBox

• Typical execution of Hemingway Homes application

Page 23: 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.

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

Page 24: 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.

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