Chapter 13 – Graphical User Interfaces: Part 2 Outline

advertisement
Chapter 13 – Graphical User Interfaces:
Part 2
Outline
13.1
13.2
13.3
13.4
13.5
13.6
13.7
13.8
13.9
13.10
13.11
Introduction
Menus
LinkLabels
ListBoxes and CheckedListBoxes
13.4.1 ListBoxes
13.4.2 CheckedListBoxes
ComboBoxes
TreeView
ListViews
Tab Control
Multiple Document Interface (MDI) Windows
Visual Inheritance
User-Defined Controls
 2002 Prentice Hall. All rights reserved.
1
2
13.1 Introduction
• Menu
– Presents several logically organized options
• LinkLabel
– Link to several destinations
• ListBox
– Manipulate a list of values
• ComboBox
– Drop-down lists
• TreeView
– Display data hierarchically
 2002 Prentice Hall. All rights reserved.
3
13.2 Menus
• Menu
– Groups related commands
– Organize without “cluttering” GUI
• Menu items
– Commands or options in menu
• Sub-menu
– Menu within a menu
• Hot keys
– Alt key shortcuts
• Press Alt + underlined letter in desired menu item
 2002 Prentice Hall. All rights reserved.
4
13.2 Menus
 2002 Prentice Hall. All rights reserved.
5
13.2 Menus
 2002 Prentice Hall. All rights reserved.
6
13.2 Menus
 2002 Prentice Hall. All rights reserved.
7
13.2 Menus
Desc rip tion / Delega te a nd Event Arguments
MainMenu a nd
MenuItem events a nd
p rop erties
MainMenu Properties
MenuItems
Lists the MenuItems that are contained in the MainMenu.
RightToLeft
Causes text to display from right to left. Useful for languages, such as
Arabic, that are read from right to left.
MenuItem Properties
Checked
Indicates whether a menu item is checked (according to property
RadioCheck). Default value is False, meaning that the menu item
is unchecked.
Index
Specifies an item’s position in its parent menu. A value of 0 places the
MenuItem at the beginning of the menu.
MenuItems
Lists the submenu items for a particular menu item.
RadioCheck
Specifies whether a selected menu item appears as a radio button
(black circle) or as a checkmark. True displays a radio button, and
False displays a checkmark; default False.
Shortcut
Specifies the shortcut key for the menu item (e.g., Ctrl + F9 is
equivalent to clicking a specific item).
ShowShortcut
Indicates whether a shortcut key is shown beside menu item text.
Default is True, which displays the shortcut key.
Text
Specifies the menu item’s text. To create an Alt access shortcut,
precede a character with & (e.g., &File for File).
Common Event
(Delegate EventHandler, event arguments EventArgs)
Click
Generated when item is clicked or shortcut key is used. This is the
default event when the menu is double-clicked in designer.
Fig. 13.4
 2002 Prentice Hall. All rights reserved.
MainMenu a nd MenuItem p rop erties a nd events.
8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
' Fig 13.5: MenuTest.vb
' Using menus to change font colors and styles.
Imports System.Windows.Forms
Outline
MenuTest.vb
Public Class FrmMenu
Inherits Form
' display label
Friend WithEvents lblDisplay As Label
' main menu (contains file and format menus)
Friend WithEvents mnuMainMenu As MainMenu
' file
Friend
Friend
Friend
menu
WithEvents mnuFile As MenuItem
WithEvents mnuitmAbout As MenuItem
WithEvents mnuitmExit As MenuItem
' format menu (contains format and font submenus)
Friend WithEvents mnuFormat As MenuItem
' color submenu
Friend WithEvents
Friend WithEvents
Friend WithEvents
Friend WithEvents
Friend WithEvents
mnuitmColor As MenuItem
mnuitmBlack As MenuItem
mnuitmBlue As MenuItem
mnuitmRed As MenuItem
mnuitmGreen As MenuItem
 2002 Prentice Hall.
All rights reserved.
9
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
' font
Friend
Friend
Friend
Friend
Friend
Friend
Friend
submenu
WithEvents
WithEvents
WithEvents
WithEvents
WithEvents
WithEvents
WithEvents
Outline
mnuitmFont As MenuItem
mnuitmTimes As MenuItem
mnuitmCourier As MenuItem
mnuitmComic As MenuItem
mnuitmDash As MenuItem
mnuitmBold As MenuItem
mnuitmItalic As MenuItem
' Visual Studio .NET generated code
MenuTest.vb
This event handler displays
a message box when the about
menu item in the file menu is clicked
' display MessageBox
Private Sub mnuitmAbout_Click( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles mnuitmAbout.Click
MessageBox.Show("This is an example" & vbCrLf & _
"of using menus.", "About", MessageBoxButtons.OK, _
MessageBoxIcon.Information)
End Sub ' mnuitmAbout_Click
This event
handler terminates the
application when the exit menu item
in the file menu is clicked
' exit program
Private Sub mnuitmExit_Click( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles mnuitmExit.Click
Application.Exit()
End Sub ' mnuitmExit_Click
 2002 Prentice Hall.
All rights reserved.
10
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
Outline
' reset font color
Private Sub ClearColor()
' clear all checkmarks
mnuitmBlack.Checked = False
mnuitmBlue.Checked = False
mnuitmRed.Checked = False
mnuitmGreen.Checked = False
End Sub ' ClearColor
MenuTest.vb
Each color menu item must be
mutually exclusive, so each event
handler calls method ClearColor
' update menu state and color display black
Private Sub mnuitmBlack_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles mnuitmBlack.Click
' reset checkmarks for color menu items
ClearColor()
' set color to black
lblDisplay.ForeColor = Color.Black
mnuitmBlack.Checked = True
End Sub ' mnuitmBlack_Click
' update menu state and color display blue
Private Sub mnuitmBlue_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles mnuitmBlue.Click
' reset checkmarks for color menu items
ClearColor()
' set color to blue
lblDisplay.ForeColor = Color.Blue
mnuitmBlue.Checked = True
End Sub ' mnuitmBlue_Click
 2002 Prentice Hall.
All rights reserved.
11
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
Outline
' update menu state and color display red
Private Sub mnuitmRed_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles mnuitmRed.Click
MenuTest.vb
' reset checkmarks for color menu items
ClearColor()
' set color to red
lblDisplay.ForeColor = Color.Red
mnuitmRed.Checked = True
End Sub ' mnuitmRed_Click
' update menu state and color display green
Private Sub mnuitmGreen_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles mnuitmGreen.Click
' reset checkmarks for color menu items
ClearColor()
' set color to green
lblDisplay.ForeColor = Color.Green
mnuitmGreen.Checked = True
End Sub ' mnuitmGreen_Click
' reset font type
Private Sub ClearFont()
Each font menu item must be
mutually exclusive, so each event
handler calls method ClearFont
' clear all checkmarks
mnuitmTimes.Checked = False
mnuitmCourier.Checked = False
mnuitmComic.Checked = False
End Sub ' ClearFont
 2002 Prentice Hall.
All rights reserved.
12
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
' update menu state and set font to Times
Private Sub mnuitmTimes_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles mnuitmTimes.Click
' reset checkmarks for font menu items
ClearFont()
Outline
MenuTest.vb
' set Times New Roman font
mnuitmTimes.Checked = True
lblDisplay.Font = New Font("Times New Roman", 30, _
lblDisplay.Font.Style)
End Sub ' mnuitmTimes_Click
' update menu state and set font to Courier
Private Sub mnuitmCourier_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles mnuitmCourier.Click
' reset checkmarks for font menu items
ClearFont()
' set Courier font
mnuitmCourier.Checked = True
lblDisplay.Font = New Font("Courier New", 30, _
lblDisplay.Font.Style)
End Sub ' mnuitmCourier_Click
' update menu state and set font to Comic Sans MS
Private Sub mnuitmComic_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles mnuitmComic.Click
' reset check marks for font menu items
ClearFont()
 2002 Prentice Hall.
All rights reserved.
13
159
' set Comic Sans font
160
mnuitmComic.Checked = True
161
lblDisplay.Font = New Font("Comic Sans MS", 30, _
162
lblDisplay.Font.Style)
163
End Sub ' mnuitmComic_Click
164
165
' toggle checkmark and toggle bold style
166
Private Sub mnuitmBold_Click( _
167
ByVal sender As System.Object, _
168
ByVal e As System.EventArgs) Handles mnuitmBold.Click
169
170
' toggle checkmark
171
mnuitmBold.Checked = Not mnuitmBold.Checked
172
173
' use Xor to toggle bold, keep all other styles
174
lblDisplay.Font = New Font( _
175
lblDisplay.Font.FontFamily, 30, _
176
lblDisplay.Font.Style Xor FontStyle.Bold)
177
End Sub ' mnuitmBold_Click
178
179
' toggle checkmark and toggle italic style
180
Private Sub mnuitmItalic_Click( _
181
ByVal sender As System.Object, _
182
ByVal e As System.EventArgs) Handles mnuitmItalic.Click
183
184
' toggle checkmark
185
mnuitmItalic.Checked = Not mnuitmItalic.Checked
186
187
' use Xor to toggle italic, keep all other styles
188
lblDisplay.Font = New Font( _
189
lblDisplay.Font.FontFamily, 30, _
190
lblDisplay.Font.Style Xor FontStyle.Italic)
191
End Sub ' mnuitmItalic_Click
192
193 End Class ' FrmMenu
Outline
MenuTest.vb
 2002 Prentice Hall.
All rights reserved.
14
13.2 Menus
 2002 Prentice Hall. All rights reserved.
15
13.3 LinkLabels
• LinkLabel
– Displays links to other resources
• Files
• Web pages
– Behavior similar to web page hyperlink
• Can change color
– New
– Previously visited
 2002 Prentice Hall. All rights reserved.
16
13.3 LinkLabels
 2002 Prentice Hall. All rights reserved.
17
13.3 LinkLabels
LinkLabel p rop erties Desc rip tion / Delega te a nd Event Arguments
a nd events
Common Properties
ActiveLinkColor
Specifies the color of the active link when clicked. Red is the default.
LinkArea
Specifies which portion of text in the LinkLabel is part of the link.
LinkBehavior
Specifies the link’s behavior, such as how the link appears when the
mouse is placed over it.
LinkColor
Specifies the original color of all links before they have been visited.
Blue is the default.
Links
Lists the LinkLabel.Link objects, which are the links contained
in the LinkLabel.
LinkVisited
If True, link appears as though it were visited (its color is changed to
that specified by property VisitedLinkColor). Default value is
False.
Text
Specifies the control’s text.
UseMnemonic
If True, & character in Text property acts as a shortcut (similar to
the Alt shortcut in menus).
VisitedLinkColor
Specifies the color of visited links. Purple is the default.
Common Event
(Delegate LinkLabelLinkClickedEventHandler, event
arguments LinkLabelLinkClickedEventArgs)
LinkClicked
Generated when the link is clicked. This is the default event when the
control is double-clicked in designer.
Fig. 13.7
LinkLabel p rop erties a nd events.
 2002 Prentice Hall. All rights reserved.
18
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
Outline
' Fig. 13.8: LinkLabelTest.vb
' Using LinkLabels to create hyperlinks.
Imports System.Windows.Forms
Public Class FrmLinkLabel
Inherits Form
' linklabels to C:\ drive, www.deitel.com and Notepad
Friend WithEvents lnklblCDrive As LinkLabel
Friend WithEvents lnklblDeitel As LinkLabel
Friend WithEvents lnklblNotepad As LinkLabel
' Visual Studio .NET generated code
Event handlers call method Start
allowing execution of other programs
from our application
' browse C:\ drive
Private Sub lnklblCDrive_LinkClicked( _
ByVal sender As System.Object, ByVal e As _
System.Windows.Forms.LinkLabelLinkClickedEventArgs) _
Handles lnklblCDrive.LinkClicked
lnklblCDrive.LinkVisited = True
System.Diagnostics.Process.Start("C:\")
End Sub ' lnklblCDrive
' load www.deitel.com in Web browser
Private Sub lnklblDeitel_LinkClicked( _
ByVal sender As System.Object, ByVal e As _
System.Windows.Forms.LinkLabelLinkClickedEventArgs) _
Handles lnklblDeitel.LinkClicked
lnklblDeitel.LinkVisited = True
System.Diagnostics.Process.Start( _
"IExplore", "http://www.deitel.com")
End Sub ' lnklblDeitel
In this event handler method Start
takes two arguments
 2002 Prentice Hall.
All rights reserved.
19
36
37
' run application Notepad
38
Private Sub lnklblNotepad_LinkClicked( _
39
ByVal sender As System.Object, ByVal e As _
40
System.Windows.Forms.LinkLabelLinkClickedEventArgs) _
41
Handles lnklblNotepad.LinkClicked
42
43
lnklblNotepad.LinkVisited = True
44
45
' run notepad application
46
' full path not needed
47
System.Diagnostics.Process.Start("notepad")
48
End Sub ' lnklblNotepad_LinkClicked
49
50 End Class ' LinkLabelList
Outline
 2002 Prentice Hall.
All rights reserved.
20
13.3 LinkLabels
 2002 Prentice Hall. All rights reserved.
21
13.3 LinkLabels
 2002 Prentice Hall. All rights reserved.
22
13.3 LinkLabels
 2002 Prentice Hall. All rights reserved.
23
13.4 ListBoxes and CheckedListBoxes
• ListBox
– View and select from multiple items
– Scroll bar appears if necessary
• CheckedListBox
– Items have checkbox
– Select multiple items at same time
– Scroll bar appears if necessary
 2002 Prentice Hall. All rights reserved.
24
13.4 ListBoxes and CheckedListBoxes
 2002 Prentice Hall. All rights reserved.
25
13.4.1 ListBoxes
ListBox p rop erties,
Desc rip tion / Delega te a nd Event Arguments
m ethod s a nd events
Common Properties
Items
The collection of items in the ListBox.
MultiColumn
Indicates whether the ListBox can break a list into multiple
columns. Multiple columns eliminate vertical scrollbars from the
display.
Returns the index of the selected item. If the user selects multiple
items, this property arbitrarily returns one of the selected indices; if no
items have been selected, the property returns -1.
SelectedIndex
SelectedIndices
Returns a collection containing the indices for all selected items.
SelectedItem
Returns a reference to the selected item (if multiple items are selected,
it returns the item with the lowest index number).
SelectedItems
Returns a collection of the selected item(s).
SelectionMode
Determines the number of items that can be selected, and the means
through which multiple items can be selected. Values None, One,
MultiSimple (multiple selection allowed) or MultiExtended
(multiple selection allowed using a combination of arrow keys or
mouse clicks and Shift and Control keys).
Sorted
Indicates whether items are sorted alphabetically. Setting this
property’s value to True sorts the items. The default value is False.
Common Method
GetSelected
Takes an index as an argument, and returns True if the corresponding
item is selected.
Common Event
(Delegate EventHandler, event arguments EventArgs)
SelectedIndexChan Generated when selected index changes. This is the default event
ged
when the control is double-clicked in the designer.
Fig. 13.10
 2002 Prentice Hall. All rights reserved.
ListBox p rop erties, method s a nd events.
26
13.4.1 ListBoxes
 2002 Prentice Hall. All rights reserved.
27
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
Outline
' Fig. 13.12: ListBoxTest.vb
' Program to add, remove and clear list box items.
Imports System.Windows.Forms
ListBoxTest.vb
Public Class FrmListBox
Inherits Form
' contains user-input list of elements
Friend WithEvents lstDisplay As ListBox
' user-input textbox
Friend WithEvents txtInput As TextBox
' add,
Friend
Friend
Friend
Friend
remove, clear and exit command buttons
WithEvents cmdAdd As Button
WithEvents cmdRemove As Button
WithEvents cmdClear As Button
WithEvents cmdExit As Button
' Visual Studio .NET generated code
' add new item (text from input box) and clear input box
Private Sub cmdAdd_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles cmdAdd.Click
lstDisplay.Items.Add(txtInput.Text)
txtInput.Text = ""
End Sub ' cmdAdd_Click
The cmdAdd_Click event handler
calls method add, which takes the
text from the text box as a String
' remove item if one is selected
Private Sub cmdRemove_Click (ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles cmdRemove.Click
 2002 Prentice Hall.
All rights reserved.
28
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
' remove only if item is selected
If lstDisplay.SelectedIndex <> -1 Then
lstDisplay.Items.RemoveAt(lstDisplay.SelectedIndex)
End If
Outline
ListBoxTest.vb
End Sub ' cmdRemove_Click
' clear all items
Private Sub cmdClear_Click (ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles cmdClear.Click
lstDisplay.Items.Clear()
End Sub ' cmdClear_Click
' exit application
Private Sub cmdExit_Click (ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles cmdExit.Click
Application.Exit()
End Sub ' cmdExit_Click
End Class ' FrmListBox
 2002 Prentice Hall.
All rights reserved.
29
13.4.1 ListBoxes
 2002 Prentice Hall. All rights reserved.
30
13.4.1 ListBoxes
 2002 Prentice Hall. All rights reserved.
31
13.4.2 CheckedListBoxes
CheckedListBox
Desc rip tion / Delega te a nd Event Arguments
p rop erties, method s
a nd events
Common Properties
(All the ListBox properties and events are inherited by
CheckedListBox.)
CheckedItems
Contains the collection of items that are checked. This is distinct from
the selected item, which is highlighted (but not necessarily checked).
[Note: There can be at most one selected item at any given time.]
CheckedIndices
Returns indices for all checked items. This is not the same as the
selected index.
SelectionMode
Determines how many items can be checked. Only possible values are
One (allows multiple checks to be placed) or None (does not allow
any checks to be placed).
Common Method
GetItemChecked
Common Event
ItemCheck
Takes an index and returns True if the corresponding item is
checked.
(Delegate ItemCheckEventHandler, event arguments
ItemCheckEventArgs)
Generated when an item is checked or unchecked.
ItemCheckEventArg
s Properties
CurrentValue
Indicates whether the current item is checked or unchecked. Possible
values are Checked, Unchecked and Indeterminate.
Index
Returns index of the item that changed.
NewValue
Specifies the new state of the item.
Fig. 13.13
CheckedListBox p rop erties, method s a nd events.
 2002 Prentice Hall. All rights reserved.
32
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
' Fig. 13.14: CheckedListBoxTest.vb
' Using the checked list boxes to add items to a list box.
Imports System.Windows.Forms
Outline
CheckedListBoxTest.v
b
Public Class FrmCheckedListBox
Inherits Form
' list of available book titles
Friend WithEvents chklstInput As CheckedListBox
' user selection list
Friend WithEvents lstDisplay As ListBox
' Visual Studio .NET generated code
' item about to change, add or remove from lstDisplay
Private Sub chklstInput_ItemCheck( _
ByVal sender As System.Object, _
ByVal e As System.Windows.Forms.ItemCheckEventArgs) _
Handles chklstInput.ItemCheck
' obtain reference of selected item
Dim item As String = chklstInput.SelectedItem
' if item checked add to listbox
' otherwise remove from listbox
If e.NewValue = CheckState.Checked Then
lstDisplay.Items.Add(item)
Else
lstDisplay.Items.Remove(item)
End If
End Sub ' chklstInput_ItemCheck
An If/Else control structure determines
whether the user checked or unchecked
an item in the CheckedListBox
 2002 Prentice Hall.
All rights reserved.
33
35
36 End Class ' FrmCheckedListBox
Outline
CheckedListBoxTest.v
b
 2002 Prentice Hall.
All rights reserved.
34
13.5 ComboBoxes
• ComboBox
– Combines TextBox features with drop-down list
– Drop-down list
• Contains a list from which a value can be selected
• Scrollbar appears if necessary
 2002 Prentice Hall. All rights reserved.
35
13.5 ComboBoxes
 2002 Prentice Hall. All rights reserved.
36
13.5 ComboBoxes
ComboBox events a nd Desc rip tion / Delega te a nd Event Arguments
p rop erties
Common Properties
DropDownStyle
Determines the type of combo box. Value Simple means that the
text portion is editable and the list portion is always visible. Value
DropDown (the default) means that the text portion is editable, but
the user must click an arrow button to see the list portion. Value
DropDownList means that the text portion is not editable and the
user must click the arrow button to see the list portion.
Items
The collection of items in the ComboBox control.
MaxDropDownItems
Specifies the maximum number of items (between 1 and 100) that the
drop-down list can display. If the number of items exceeds the
maximum number of items to display, a scrollbar appears.
SelectedIndex
Returns the index of the selected item. If there is no selected item, -1
is returned.
SelectedItem
Returns a reference to the selected item.
Sorted
Indicates whether items are sorted alphabetically. Setting this
property’s value to True sorts the items. Default is False.
Common Event
(Delegate EventHandler, event arguments EventArgs)
SelectedIndexChan Generated when the selected index changes (such as when a different
ged
item is selected). This is the default event when control is doubleclicked in designer.
Fig. 13.16
ComboBox p rop erties a nd events.
 2002 Prentice Hall. All rights reserved.
37
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
Outline
' Fig. 13.17: ComboBoxTest.vb
' Using ComboBox to select shape to draw.
Imports System.Windows.Forms
Imports System.Drawing
Public Class FrmComboBox
Inherits Form
' contains shape list (circle, square, ellipse, pie)
Friend WithEvents cboImage As ComboBox
' Visual Studio .NET generated code
' get selected index, draw shape
Private Sub cboImage_SelectedIndexChanged( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles cboImage.SelectedIndexChanged
Event handler
cboImage_SelectedIndexChange
handles the SelectedIndexChange
events generated when a user
selects an item from the combo box
' create graphics object, pen and brush
Dim myGraphics As Graphics = MyBase.CreateGraphics()
' create Pen using color DarkRed
Dim myPen As New Pen(Color.DarkRed)
' create SolidBrush using color DarkRed
Dim mySolidBrush As New SolidBrush(Color.DarkRed)
' clear drawing area by setting it to colorAWhite
select
myGraphics.Clear(Color.White)
' find index, draw proper shape
Select Case cboImage.SelectedIndex
case structure
is used to determine
what item was selected
 2002 Prentice Hall.
All rights reserved.
38
36
Case 0 ' case circle is selected
37
myGraphics.DrawEllipse(myPen, 50, 50, 150, 150)
38
39
Case 1 ' case rectangle is selected
40
myGraphics.DrawRectangle(myPen, 50, 50, 150, 150)
41
42
Case 2 ' case ellipse is selected
43
myGraphics.DrawEllipse(myPen, 50, 85, 150, 115)
44
45
Case 3 ' case pie is selected
46
myGraphics.DrawPie(myPen, 50, 50, 150, 150, 0, 45)
47
48
Case 4 ' case filled circle is selected
49
myGraphics.FillEllipse( _
50
mySolidBrush, 50, 50, 150, 150)
51
52
Case 5 ' case filled rectangle is selected
53
myGraphics.FillRectangle( _
54
mySolidBrush, 50, 50, 150, 150)
55
56
Case 6 ' case filled ellipse is selected
57
myGraphics.FillEllipse( _
58
mySolidBrush, 50, 85, 150, 115)
59
60
Case 7 ' case filled pie is selected
61
myGraphics.FillPie( _
62
mySolidBrush, 50, 50, 150, 150, 0, 45)
63
64
End Select
65
66
End Sub ' cboImage_SelectedIndexChanged
67
68 End Class ' FrmComboBox
Outline
 2002 Prentice Hall.
All rights reserved.
39
13.5 ComboBoxes
 2002 Prentice Hall. All rights reserved.
40
13.5 ComboBoxes
 2002 Prentice Hall. All rights reserved.
41
13.8 Tab Control
• TabControl
– Creates tabbed windows
– Saves space using TabPage objects
• TabPage objects
– Similar to Panels and GroupBoxes
– Can contain controls
 2002 Prentice Hall. All rights reserved.
42
13.8 Tab Control
 2002 Prentice Hall. All rights reserved.
43
13.8 Tab Control
 2002 Prentice Hall. All rights reserved.
44
13.8 Tab Control
 2002 Prentice Hall. All rights reserved.
45
13.8 Tab Control
TabControl p rop erties
Desc rip tion / Delega te a nd Event Arguments
a nd events
Common Properties
ImageList
Specifies images to be displayed on tabs.
ItemSize
Specifies tab size.
MultiLine
Indicates whether multiple rows of tabs can be displayed.
SelectedIndex
Index of selected TabPage.
SelectedTab
The selected TabPage.
TabCount
Returns the number of tab pages.
TabPages
Collection of TabPages within the TabControl.
Common Event
(Delegate EventHandler, event arguments EventArgs)
SelectedIndexChanged
Generated when SelectedIndex changes (i.e., another
TabPage is selected).
Fig. 13.29
TabControl p rop erties a nd events.
 2002 Prentice Hall. All rights reserved.
46
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
' Fig. 13.30: UsingTabs.vb
' Using TabControl to display various font settings.
Outline
Imports System.Windows.Forms
Public Class FrmTabs
Inherits Form
' output label reflects text changes
Friend WithEvents lblDisplay As Label
' table control containing table pages tbpColor,
' tbpSize, tbpMessage and tbpAbout
Friend WithEvents tbcTextOptions As TabControl
' table page containing color options
Friend WithEvents tbpColor As TabPage
Friend WithEvents radBlack As RadioButton
Friend WithEvents radRed As RadioButton
Friend WithEvents radGreen As RadioButton
' table page containing font size options
Friend WithEvents tbpSize As TabPage
Friend WithEvents radSize12 As RadioButton
Friend WithEvents radSize16 As RadioButton
Friend WithEvents radSize20 As RadioButton
' table page containing text display options
Friend WithEvents tbpMessage As TabPage
Friend WithEvents radHello As RadioButton
Friend WithEvents radGoodbye As RadioButton
' table page containing about message
Friend WithEvents tbpAbout As TabPage
Friend WithEvents lblMessage As Label
 2002 Prentice Hall.
All rights reserved.
47
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
' Visual Studio .NET generated code
Outline
' event handler for black radio button
Private Sub radBlack_CheckedChanged( _
ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles radBlack.CheckedChanged
lblDisplay.ForeColor = Color.Black
End Sub ' radBlack_CheckedChanged
' event handler for red radio button
Private Sub radRed_CheckedChanged( _
ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles radRed.CheckedChanged
lblDisplay.ForeColor = Color.Red
End Sub ' radRed_CheckedChanged
' event handler for green radio button
Private Sub radGreen_CheckedChanged( _
ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles radGreen.CheckedChanged
lblDisplay.ForeColor = Color.Green
End Sub ' radGreen_CheckedChanged
' event handler for size 12 radio button
Private Sub radSize12_CheckedChanged( _
ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles radSize12.CheckedChanged
lblDisplay.Font = New Font(lblDisplay.Font.Name, 12)
End Sub ' radSize12_CheckedChanged
 2002 Prentice Hall.
All rights reserved.
48
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
' event handler for size 16 radio button
Private Sub radSize16_CheckedChanged( _
ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles radSize16.CheckedChanged
Outline
lblDisplay.Font = New Font(lblDisplay.Font.Name, 16)
End Sub ' radSize16_CheckedChanged
' event handler for size 20 radio button
Private Sub radSize20_CheckedChanged( _
ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles radSize20.CheckedChanged
lblDisplay.Font = New Font(lblDisplay.Font.Name, 20)
End Sub ' radSize20_CheckedChanged
' event handler for message "Hello!" radio button
Private Sub radHello_CheckedChanged( _
ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles radHello.CheckedChanged
lblDisplay.Text = "Hello!"
End Sub ' radHello_CheckedChanged
' event handler for message "Goodbye!" radio button
Private Sub radGoodbye_CheckedChanged( _
ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles radGoodbye.CheckedChanged
lblDisplay.Text = "Goodbye!"
End Sub ' radGoodbye_CheckedChanged
End Class ' FrmTabs
 2002 Prentice Hall.
All rights reserved.
49
13.8 Tab Control
 2002 Prentice Hall. All rights reserved.
50
13.9 Multiple Document Interface (MDI)
Windows
• MDI
– Allows multiple windows
• Parent window
– Application window
– Can have many child windows
• Child window
– Cannot be parent
– Has exactly one parent
– Cannot be moved outside parent
– Functionality can be different than other child windows
from same parent
 2002 Prentice Hall. All rights reserved.
51
13.9 Multiple Document Interface (MDI)
Windows
Single Document Interface
(SDI)
 2002 Prentice Hall. All rights reserved.
Multiple Document Interface
(MDI)
52
13.9 Multiple Document Interface (MDI)
Windows
MDI Form events a nd
p rop erties
Common MDI Child
Properties
IsMdiChild
MdiParent
Common MDI Parent
Properties
ActiveMdiChild
Desc rip tion / Delega te a nd Event Arguments
Indicates whether the Form is an MDI child. If True, Form is an
MDI child (read-only property).
Specifies the MDI parent Form of the child.
Returns the Form that is the currently active MDI child (returns
Nothing if no children are active).
IsMdiContainer
Indicates whether a Form can be an MDI parent. If True, the Form
can be an MDI parent. The default value is False.
MdiChildren
Returns the MDI children as an array of Forms.
Common Method
LayoutMdi
Determines the display of child forms on an MDI parent. Takes as a
parameter an MdiLayout enumeration with possible values
ArrangeIcons, Cascade, TileHorizontal and
TileVertical. Figure 13.36 depicts the effects of these values.
Common Event
(Delegate EventHandler, event arguments EventArgs)
MdiChildActivate
Generated when an MDI child is closed or activated.
Fig. 13.33 MDI p a rent a nd MDI c hild events a nd p rop erties.
 2002 Prentice Hall. All rights reserved.
53
13.9 Multiple Document Interface (MDI)
Windows
 2002 Prentice Hall. All rights reserved.
54
13.9 Multiple Document Interface (MDI)
Windows
 2002 Prentice Hall. All rights reserved.
55
13.9 Multiple Document Interface (MDI)
Windows
 2002 Prentice Hall. All rights reserved.
56
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
' Fig. 13.37: UsingMDI.vb
' Demonstrating use of MDI parent and child windows.
Outline
Imports System.Windows.Forms
Public Class FrmUsingMDI
Inherits Form
' main menu containing menu items File and Window
Friend WithEvents mnuMain As MainMenu
' menu containing submenu New and menu item Exit
Friend WithEvents mnuitmFile As MenuItem
Friend WithEvents mnuitmExit As MenuItem
' submenu New
Friend WithEvents
Friend WithEvents
Friend WithEvents
Friend WithEvents
' menu containing
' TileVertical
Friend WithEvents
Friend WithEvents
Friend WithEvents
Friend WithEvents
mnuitmNew As
mnuitmChild1
mnuitmChild2
mnuitmChild3
MenuItem
As MenuItem
As MenuItem
As MenuItem
menu items Cascade, TileHorizontal and
mnuitmWindow As MenuItem
mnuitmCascade As MenuItem
mnuitmTileHorizontal As MenuItem
mnuitmTileVertical As MenuItem
' Visual Studio .NET generated code
' create Child1 when menu clicked
Private Sub mnuitmChild1_Click( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles mnuitmChild1.Click
 2002 Prentice Hall.
All rights reserved.
57
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
' create image path
Dim imagePath As String = _
Directory.GetCurrentDirectory() & "\images\image0.jpg"
Outline
' create new child
childWindow = New FrmChild(imagePath, "Child1")
childWindow.MdiParent = Me ' set parent
childWindow.Show()
' display child
End Sub ' mnuitmChild1_Click
' create Child2 when menu clicked
Private Sub mnuitmChild2_Click( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles mnuitmChild2.Click
' create image path
Dim imagePath As String = _
Directory.GetCurrentDirectory() & "\images\image1.jpg"
' create new child
childWindow = New FrmChild(imagePath, "Child2")
childWindow.MdiParent = Me ' set parent
childWindow.Show()
' display child
End Sub ' mnuitmChild2_Click
' create Child3 when menu clicked
Private Sub mnuitmChild3_Click( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles mnuitmChild3.Click
' create image path
Dim imagePath As String = _
Directory.GetCurrentDirectory() & "\images\image2.jpg"
 2002 Prentice Hall.
All rights reserved.
58
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
' create new child
childWindow = New FrmChild(imagePath, "Child3")
childWindow.MdiParent = Me ' set parent
childWindow.Show()
' display child
End Sub ' mnuitmChild3_Click
Outline
' exit application
Private Sub mnuitmExit_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles mnuitmExit.Click
Application.Exit()
End Sub ' mnuitmExit_Click
' set cascade layout
Private Sub mnuitmCascade_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles mnuitmCascade.Click
Me.LayoutMdi(MdiLayout.Cascade)
End Sub ' mnuitmCascade_Click
Method LayoutMdi can take
values ArrangeIcons, Cascade,
TileHorizontal and TileVertical
' set TileHorizontal layout
Private Sub mnuitmTileHorizontal_Click( _
ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles mnuitmTileHorizontal.Click
Me.LayoutMdi(MdiLayout.TileHorizontal)
End Sub ' mnuitmTileHorizontal_Click
' set TileVertical layout
Private Sub mnuitmTileVertical_Click( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles mnuitmTileVertical.Click
 2002 Prentice Hall.
All rights reserved.
59
103
Me.LayoutMdi(MdiLayout.TileVertical)
104
End Sub ' mnuitmTileVertical_Click
105
106 End Class ' FrmUsingMDI
Outline
 2002 Prentice Hall.
All rights reserved.
60
13.9 Multiple Document Interface (MDI)
Windows
 2002 Prentice Hall. All rights reserved.
61
13.10 Visual Inheritance
• Allows creation of forms by inheriting from other
forms
• Derived forms contain same functionality as base
form including:
–
–
–
–
–
Properties
Methods
Variables
Controls
All visual aspects
•
•
•
•
Sizing
Layout
Spacing
Colors and fonts
 2002 Prentice Hall. All rights reserved.
62
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
' Fig. 13.39: FrmInheritance.vb
' Form template for use with visual inheritance.
Imports System.Windows.Forms
Outline
FrmInheritance.vb
Public Class FrmInheritance
Inherits Form
Friend WithEvents lblBug As Label
' top label
Friend WithEvents lblCopyright As Label ' bottom label
Friend WithEvents cmdLearn As Button
' left button
' Visual Studio .NET generated code
' invoked when user clicks Learn More button
Private Sub cmdLearn_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles cmdLearn.Click
Method cmdLearn_Click
is invoked
MessageBox.Show("Bugs, Bugs, Bugs is a product of " & _
" Bug2Bug.com.", "Learn More", MessageBoxButtons.OK, _
MessageBoxIcon.Information)
End Sub ' cmdLearn_Click
End Class ' FrmInheritance
 2002 Prentice Hall.
All rights reserved.
63
13.10 Visual Inheritance
 2002 Prentice Hall. All rights reserved.
64
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
Outline
' Fig. 13.41: VisualTest.vb
' A form that uses visual inheritance.
Public Class FrmVisualTest
Inherits VisualForm.FrmInheritance
' new button added to form
Friend WithEvents cmdProgram As Button
' Visual Studio .NET generated code
VisualTest.vb
Components, layout and
functionality of the base form
are inherited by the new form
' invoke when user clicks Learn the Program button
Private Sub cmdProgram_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles cmdProgram.Click
MessageBox.Show( _
"This program was created by Deitel & Associates", _
"Learn the Program", MessageBoxButtons.OK, _
MessageBoxIcon.Information)
End Sub ' cmdProgram_Click
End Class ' FrmVisualTest
 2002 Prentice Hall.
All rights reserved.
65
13.10 Visual Inheritance
 2002 Prentice Hall. All rights reserved.
66
13.11 User-Defined Controls
• Custom controls
– Techniques
• Inherit from class Control
– Define a brand new control
• Inherit from Windows Forms control
– Add functionality to preexisting control
• Create a UserControl
– Multiple preexisting controls
 2002 Prentice Hall. All rights reserved.
67
13.11 User-Defined Controls
Custom Control Tec hniq ues a nd Desc rip tion
PaintEventArgs Prop erties
Inherit from Windows Forms
control
Add functionality to a preexisting control. If overriding
method OnPaint, call base class OnPaint. Can add only to
the original control appearance, not redesign it.
Create a UserControl
Create a UserControl composed of multiple preexisting
controls (and combine their functionality). Cannot override
OnPaint methods of custom controls. Instead, add drawing
code to a Paint event handler. Can add only to the original
control appearance, not redesign it.
Inherit from class Control
Define a brand-new control. Override OnPaint method, call
base class method OnPaint and include methods to draw the
control. Can customize control appearance and functionality.
PaintEventArgs Properties
Use this object inside method OnPaint or Paint to draw
on the control.
Graphics
The graphics object of the control. Used to draw on the
control.
ClipRectangle
Specifies the rectangle indicating the boundary of the control.
Fig. 13.42 Custom c ontrol c rea tion.
 2002 Prentice Hall. All rights reserved.
68
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
' Fig 13.43: CClockUserControl.vb
' User-defined control with timer and label.
Imports System.Windows.Forms
' create clock control that inherits from UserControl
Public Class CClockUserControl
Inherits UserControl
Outline
CClockUserControl.v
b
' displays time
Friend WithEvents lblDisplay As Label
' non-visible event-triggering timer object
Friend WithEvents tmrClock As Timer
' Visual Studio .NET generated code
' update label at every tick
Private Sub tmrClock_Tick(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles tmrClock.Tick
' get current time (Now), convert to string
lblDisplay.Text = DateTime.Now.ToLongTimeString
End Sub ' tmrClock_Tick
End Class ' CClockUserControl
 2002 Prentice Hall.
All rights reserved.
69
13.11 User-Defined Controls
 2002 Prentice Hall. All rights reserved.
70
13.11 User-Defined Controls
 2002 Prentice Hall. All rights reserved.
71
13.11 User-Defined Controls
 2002 Prentice Hall. All rights reserved.
72
13.11 User-Defined Controls
 2002 Prentice Hall. All rights reserved.
73
13.11 User-Defined Controls
 2002 Prentice Hall. All rights reserved.
Download