Documente Academic
Documente Profesional
Documente Cultură
1
5Graphical User
Interfaces with
Windows Forms:
Part 2
OBJECTIVES
In this chapter you will learn:
To create menus, tabbed windows and multiple
document interface (MDI) programs.
To use the ListView and TreeView controls for
displaying information.
To create hyperlinks using the LinkLabel control.
To display lists of information in ListBox and
ComboBox controls.
To input date and time data with the
DateTimePicker.
To create custom controls.
15.1
15.2
15.3
15.4
15.5
15.6
15.7
15.8
15.9
15.10
15.11
15.12
15.13
15.14
Introduction
Menus
MonthCalendar Control
DateTimePicker Control
LinkLabel Control
ListBox Control
CheckedListBox Control
ComboBox Control
TreeView Control
ListView Control
TabControl Control
15.1 Introduction
Visual Studio provides a large set of GUI components.
Visual Studio also enables you to design custom
controls and add them to the ToolBox
15.2 Menus
Menus provide groups of related commands (Fig. 15.1).
Menus organize commands without cluttering the GUI.
Menu
Menu items
Submenu
Separator bar
Shortcut key
Menu icons
Disabled commands
Main menu
bar
MenuStrip icon
adding items
to
the menu
10
Setting modifier
keys
Select key (modifier and key
combination specifies the
shortcut key for the menu
item)
11
12
13
14
Fig. 15.6 | MenuStrip and ToolStripMenuItem properties and an event. (Part 1 of 2.)
15
Fig. 15.6 | MenuStrip and ToolStripMenuItem properties and an event. (Part 2 of 2.)
Outline
16
MenuTestForm.cs
( 1 of 10 )
Outline
17
MenuTestForm.cs
( 2 of 10 )
Fig. 15.7 | Menus for changing text font and color. (Part 1 of 9.)
Outline
18
MenuTestForm.cs
( 3 of 10 )
Fig. 15.7 | Menus for changing text font and color. (Part 2 of 9.)
Outline
19
MenuTestForm.cs
( 4 of 10 )
Fig. 15.7 | Menus for changing text font and color. (Part 3 of 9.)
Outline
20
MenuTestForm.cs
( 5 of 10 )
Fig. 15.7 | Menus for changing text font and color. (Part 4 of 9.)
Outline
21
MenuTestForm.cs
( 6 of 10 )
Each Color menu item calls
ClearColor before setting its
Checked property (making the
checks mutually exclusive).
Fig. 15.7 | Menus for changing text font and color. (Part 5 of 9.)
Outline
22
MenuTestForm.cs
( 7 of 10 )
Fig. 15.7 | Menus for changing text font and color. (Part 6 of 9.)
Outline
23
MenuTestForm.cs
( 8 of 10 )
Fig. 15.7 | Menus for changing text font and color. (Part 7 of 9.)
Outline
24
MenuTestForm.cs
( 9 of 10 )
The Bold and Italic
menu items use the
bitwise logical
exclusive OR operator
to combine font styles.
Fig. 15.7 | Menus for changing text font and color. (Part 8 of 9.)
a) Application at
start
Outline
b) Changing font to
Bold
25
MenuTestForm.cs
( 10 of 10 )
c) Application with bold
font
d) Changing font to
Red
Fig. 15.7 | Menus for changing text font and color. (Part 9 of 9.)
26
27
28
29
30
31
Outline
32
DateTimePicker
Form.cs
(1 of 5 )
Outline
33
DateTimePicker
Form.cs
(2 of 5 )
Outline
34
DateTimePicker
Form.cs
(3 of 5 )
Outline
35
DateTimePicker
Form.cs
(4 of 5 )
Setting the
MinDate and
MaxDate
properties to keep
drop-off sometime
in the next year.
Outline
a) Clicking the down arrow
36
DateTimePicker
Form.cs
(5 of 5 )
37
LinkLabel on a Form
38
39
Outline
40
LinkLabelTest
Form.cs
(1 of 6 )
Outline
41
LinkLabelTest
Form.cs
(2 of 6 )
Setting the LinkVisited property to
true, changing the links color to
purple.
Opening a Windows Explorer window.
(the @ symbol indicates that characters in
the string should be interpreted
literally).
Opening a web
page in the users
default web
browser.
Outline
42
LinkLabelTest
Form.cs
(3of 6 )
Opening an application.
Windows recognizes the
argument without a directory
or file extension.
Outline
43
LinkLabelTest
Form.cs
(4 of 6 )
Outline
44
LinkLabelTest
Form.cs
(5 of 6 )
Click second
LinkLabel to go to
Deitel website
Outline
45
LinkLabelTest
Form.cs
Click on third
LinkLabel
to open Notepad
(6 of 6 )
46
47
Selected
items
Scrollbars
appear if
necessary
Checked item
CheckedListBox
48
49
50
Outline
51
ListBoxTest
Form.cs
(1 of 5 )
Fig. 15.18 | Program that adds, removes and clears ListBox items. (Part 1 of 5.)
Outline
52
ListBoxTest
Form.cs
(2 of 5 )
Adding strings using
method Add of the Items
collection.
Using method
RemoveAt to remove
the item at the selected
index.
Fig. 15.18 | Program that adds, removes and clears ListBox items. (Part 2 of 5.)
Outline
53
ListBoxTest
Form.cs
(3 of 5 )
Using method Clear
of the Items collection
to remove all the
entries.
Fig. 15.18 | Program that adds, removes and clears ListBox items. (Part 3 of 5.)
Outline
54
ListBoxTest
Form.cs
a) Adding an item
(4 of 5 )
Fig. 15.18 | Program that adds, removes and clears ListBox items. (Part 4 of 5.)
Outline
55
ListBoxTest
Form.cs
c) An item has been removed
(5 of 5 )
Fig. 15.18 | Program that adds, removes and clears ListBox items. (Part 5 of 5.)
56
57
58
Outline
59
CheckedListBox
TestForm.cs
(1 of 4 )
Outline
60
CheckedListBox
TestForm.cs
(2 of 4 )
Determining whether
the user checked or
unchecked the item.
Outline
61
CheckedListBox
TestForm.cs
(3 of 4 )
a) Application at start
Outline
62
CheckedListBox
TestForm.cs
(4 of 4 )
c) Unchecking selected items
d) Checking items
63
64
65
Outline
Class ComboBoxTestForm (Fig. 15.23) allows users to
select a shape to draw by using a ComboBox.
Set the ComboBoxs DropDownStyle to DropDownList
to restrict users to preset options.
66
ComboBoxTest
Form.cs
( 1 of 5 )
Outline
67
ComboBoxTest
Form.cs
( 2 of 5 )
The Graphics object allows
a pen or brush to draw on a
component
The Pen object is used to draw
the outlines of shapes.
The SolidBrush object is
used to fill solid shapes.
Outline
68
ComboBoxTest
Form.cs
( 3 of 5 )
SelectedIndex
determines which item the
user selected.
DrawEllipse takes a Pen,
the coordinates of the center
and the dimensions of the
ellipse.
DrawRectangle takes a
Pen, the coordinates of the
upper-left corner and the
dimensions of the rectangle.
Outline
69
ComboBoxTest
Form.cs
( 4 of 5 )
a)
b)
Outline
70
ComboBoxTest
Form.cs
( 5 of 5 )
c)
d)
71
72
Click + to expand
node and display
child nodes
73
74
75
76
77
Outline
Class TreeViewDirectoryStructureForm
(Fig. 15.28) uses a TreeView to display the contents of a
directory.
78
TreeViewDirectory
StructureForm
.Cs
( 1 of 6 )
Outline
79
TreeViewDirectory
StructureForm
.Cs
( 2 of 6 )
Method GetDirectories
takes the given directory and
returns an array of strings
(the subdirectories).
Outline
80
TreeViewDirectory
StructureForm
.Cs
( 3 of 6 )
Outline
81
TreeViewDirectory
StructureForm
.Cs
( 4 of 6 )
Outline
82
TreeViewDirectory
StructureForm
.Cs
( 5 of 6 )
Outline
83
TreeViewDirectory
StructureForm
.Cs
a)
b)
( 6 of 6 )
84
85
86
87
88
Outline
Class ListViewTestForm (Fig. 15.31) displays files and
folders in a ListView.
If a file or folder is inaccessible because of permission
settings, a MessageBox appears.
89
ListViewTest
Form.cs
( 1 of 8 )
Outline
90
ListViewTest
Form.cs
( 2 of 8 )
Outline
91
ListViewTest
Form.cs
( 3 of 8 )
Using property Parent to
return the parent directory.
Outline
92
ListViewTest
Form.cs
( 4 of 8 )
Outline
93
ListViewTest
Form.cs
( 5 of 8 )
Method GetDirectories
returns an array of
DirectoryInfo objects
representing the subdirectories.
Method GetFiles returns an
array of class FileInfo
objects.
Iterating through
subdirectories and adding
them to
browserListView.
Outline
94
ListViewTest
Form.cs
( 6 of 8 )
Iterating through
subdirectories and
adding them to
browserListView.
Outline
95
ListViewTest
Form.cs
( 7 of 8 )
Outline
96
a)
ListViewTest
Form.cs
( 8 of 8 )
b)
c)
97
98
Tabs
99
100
TabPage
Controls in TabPage
TabControl
101
102
103
Outline
Class UsingTabsForm (Fig. 15.36) uses a TabControl
to display various options relating to the text on a label
(Color, Size and Message).
104
UsingTabsForm.cs
( 1 of 6 )
Fig. 15.36 | TabControl used to display various font settings. (Part 1 of 6.)
Outline
105
UsingTabsForm.cs
( 2 of 6 )
Fig. 15.36 | TabControl used to display various font settings. (Part 2 of 6.)
Outline
106
UsingTabsForm.cs
( 3 of 6 )
Fig. 15.36 | TabControl used to display various font settings. (Part 3 of 6.)
Outline
107
UsingTabsForm.cs
( 4 of 6 )
Fig. 15.36 | TabControl used to display various font settings. (Part 4 of 6.)
Outline
108
UsingTabsForm.cs
( 5 of 6 )
a) Color tab
b) Size tab
Fig. 15.36 | TabControl used to display various font settings. (Part 5 of 6.)
Outline
109
UsingTabsForm.cs
( 6 of 6 )
c) Message tab
d) About tab
Fig. 15.36 | TabControl used to display various font settings. (Part 6 of 6.)
110
111
112
MDI parent
MDI child
MDI child
113
114
115
Fig. 15.39 | MDI parent and MDI child properties, method and event.
116
Figure 15.40 shows two images: one containing two minimized child
windows and a second containing a maximized child window.
Parent-window icons:
minimize, maximize and close
117
118
119
120
b) Cascade
c) TileHorizontal
d) TileVertical
121
Outline
122
Outline
123
UsingMDIForm.cs
( 2 of 6 )
Outline
124
UsingMDIForm.cs
( 3 of 6 )
Outline
125
UsingMDIForm.cs
( 4 of 6 )
Setting the layout of child
Forms.
Outline
126
UsingMDIForm.cs
( 5 of 6 )
a) Creating a child window
Outline
127
UsingMDIForm.cs
( 6 of 6 )
Outline
Define the MDI child class by right clicking the project in the
Solution Explorer and selecting Add > Windows
Form.
Name the new class ChildForm (Fig. 15.44).
128
ChildForm.cs
( 1 of 2 )
Outline
129
ChildForm.cs
( 2 of 2 )
Retrieving the
specified image
resource and
displaying it.
130
Outline
131
VisualInheritance
BaseForm.cs
( 1 of 2 )
Outline
132
VisualInheritance
BaseForm.cs
( 2 of 2 )
133
134
Outline
135
Inheriting the
Forms properties
and appearance.
Outline
136
VisualInheritance
TestForm.cs
( 2 of 2 )
Derived class
cannot modify
these controls.
137
138
139
Outline
Create a UserControl class for the project by selecting
Project > Add User Control .
We name the file (and the class) ClockUserControl.
Add a Label and a Timer to the UserControl.
Set the Timer interval to 1000 milliseconds.
140
ClockUserControl
.cs
( 1 of 2 )
Outline
141
ClockUserControl
.cs
( 2 of 2 )
DateTime.Now is the
current time.
Method
ToLongTimeString
converts Now to a
string.
142
143
144
145
146
Newly inserted
control