CSL13GUI2
CSL13GUI2
13.1 Introduction
1
3
13.2 Menus
13.2 Menus
Menu Shortcut
key
Disabled
command
submenu
Separator bar
Checked
menu item
2
5
13.2 Menus
MainMenu icon
13.2 Menus
3
7
13.2 Menus
MergeOrder This property sets the position of menu item when parent menu merged
with another menu.
MergeType This property takes a value of the MenuMerge enumeration. Specifies
how parent menu merges with another menu. Possible values are Add,
MergeItems, Remove and Replace.
RadioCheck If true, menu item appears as radio button (black circle) when
checked; if false, menu item displays checkmark. Default false.
Shortcut Shortcut key for the menu item (i.e. Ctrl + F9 can be equivalent to
clicking a specific item).
ShowShortcut If true, shortcut key shown beside menu item text. Default true.
Text Text to appear on menu item. To make an Alt access shortcut, precede
a character with & (i.e. &File for File).
Common Events (Delegate EventHandler, event arguments EventArgs)
Click Raised when item is clicked or shortcut key is used. Default when
double-clicked in designer.
Fig. 13.3 MainMenu a nd MenuItem p rop erties a nd e ve nts.
8
1 // Fig 13.4: [Link] Outline
2 // Using menus to change font colors and styles.
3
4 using System;
5 using [Link]; [Link]
6 using [Link];
7 using [Link];
8 using [Link];
9 using [Link];
10
11 public class MenuTest : [Link]
12 {
13 // display label
14 private [Link] displayLabel;
15
16 // main menu (contains file and format menu)
17 private [Link] mainMenu;
18
19 // file menu
20 private [Link] fileMenuItem; About command
21 private [Link] aboutMenuItem; Exit command
22 private [Link] exitMenuItem;
23
24 // format menu
25 private [Link] formatMenuItem;
26
27 // color submenu
28 private [Link] colorMenuItem;
29 private [Link] blackMenuItem; Color options
30 private [Link] blueMenuItem;
31 private [Link] redMenuItem;
32 private [Link] greenMenuItem;
33
© 2002 Prentice Hall.
All rights reserved.
4
9
34 // font submenu Outline
35 private [Link] timesMenuItem;
36 private [Link] courierMenuItem;
37 private [Link] comicMenuItem;
38 private [Link] boldMenuItem; [Link]
39 private [Link] italicMenuItem; Font options
40 private [Link] fontMenuItem;
41
42 private [Link] separatorMenuItem; Style options
43
44 [STAThread]
45 static void Main()
46 {
47 [Link]( new MenuTest() );
48 }
49
50 // display MessageBox About event
51 private void aboutMenuItem_Click(
52 object sender, [Link] e )
handler
53 {
54 [Link](
55 "This is an example\nof using menus.",
56 "About", [Link],
57 [Link] );
58 }
59
60 // exit program
61 private void exitMenuItem_Click( Exit event
62 object sender, [Link] e ) Handler
63 {
64 [Link]();
65 }
66
© 2002 Prentice Hall.
All rights reserved.
10
67 // reset color Outline
68 private void ClearColor()
69 {
70 // clear all checkmarks
71 [Link] = false; [Link]
72 [Link] = false;
73 [Link] = false;
74 [Link] = false;
75 }
76
77 // update menu state and color display black
78 private void blackMenuItem_Click( Black event
79 object sender, [Link] e )
80 {
handler
81 // reset checkmarks for color menu items
82 ClearColor();
83
84 // set color to black
85 [Link] = [Link];
86 [Link] = true;
87 }
88
89 // update menu state and color display blue
90 private void blueMenuItem_Click(
91 object sender, [Link] e ) Blue event
92 {
93 // reset checkmarks for color menu items
Handler
94 ClearColor();
95
96 // set color to blue
97 [Link] = [Link];
98 [Link] = true;
99 }
100
© 2002 Prentice Hall.
All rights reserved.
5
11
101 // update menu state and color display red Outline
102 private void redMenuItem_Click(
103 object sender, [Link] e )
104 { Red event
[Link]
105 // reset checkmarks for color menu items handler
106 ClearColor();
107
108 // set color to red
109 [Link] = [Link];
110 [Link] = true;
111 }
112
113 // update menu state and color display green
114 private void greenMenuItem_Click(
115 object sender, [Link] e )
116 {
Green event
117 // reset checkmarks for color menu items handler
118 ClearColor();
119
120 // set color to green
121 [Link] = [Link];
122 [Link] = true;
123 }
124
125 // reset font types
126 private void ClearFont()
127 {
128 // clear all checkmarks
129 [Link] = false;
130 [Link] = false;
131 [Link] = false;
132 }
133
© 2002 Prentice Hall.
All rights reserved.
12
134 // update menu state and set font to Times Outline
135 private void timesMenuItem_Click(
136 object sender, [Link] e )
137 {
Times New Roman
138 // reset checkmarks for font menu items event handler [Link]
139 ClearFont();
140
141 // set Times New Roman font
142 [Link] = true;
143 [Link] = new Font(
144 "Times New Roman", 14, [Link] );
145 }
146
147 // update menu state and set font to Courier
148 private void courierMenuItem_Click(
149 object sender, [Link] e ) Courier New
150 { event handler
151 // reset checkmarks for font menu items
152 ClearFont();
153
154 // set Courier font
155 [Link] = true;
156 [Link] = new Font(
157 "Courier New", 14, [Link] );
158 }
159
160 // update menu state and set font to Comic Sans MS
161 private void comicMenuItem_Click( Comic Sans
162 object sender, [Link] e ) event handler
163 {
164 // reset checkmarks for font menu items
165 ClearFont();
166
© 2002 Prentice Hall.
All rights reserved.
6
13
167 // set Comic Sans font Outline
168 [Link] = true;
169 [Link] = new Font(
170 "Comic Sans MS", 14, [Link] );
171 } [Link]
172
173 // toggle checkmark and toggle bold style
174 private void boldMenuItem_Click( Bold event
175 object sender, [Link] e ) handler
176 {
177 // toggle checkmark
178 [Link] = ![Link];
179
180 // use Xor to toggle bold, keep all other styles
181 [Link] = new Font(
182 [Link], 14,
183 [Link] ^ [Link] );
184 }
185
186 // toggle checkmark and toggle italic style
187 private void italicMenuItem_Click( Italic event
188 object sender, [Link] e)
189 { handler
190 // toggle checkmark
191 [Link] = ![Link];
192
193 // use Xor to toggle bold, keep all other styles
194 [Link] = new Font(
195 [Link], 14,
196 [Link] ^ [Link] );
197 }
198
199 } // end class MenuTest
© 2002 Prentice Hall.
All rights reserved.
14
Outline
[Link]
Program Output
7
15
13.3 LinkLabels
16
13.3 LinkLabels
LinkLabel
on a form
Hand image
displayed when
mouse cursor over
a LinkLabel
Fig. 13.5 LinkLabel control in the design phase and in running program.
8
17
13.3 LinkLabels
L i n k L a be l p ro p e rt ie s D e sc rip t io n / D e le g a t e a n d Ev e n t A rg u m e n t s
a nd e ve nts
C om m on Properties
18
1 // Fig. 13.7: [Link] Outline
2 // Using LinkLabels to create hyperlinks.
3
4 using System;
5 using [Link]; [Link]
6 using [Link];
7 using [Link];
8 using [Link];
9 using [Link];
10
11 public class LinkLabelTest : [Link]
12 {
13 // linklabels to C: drive, [Link] and Notepad
14 private [Link] driveLinkLabel; C drive link
15 private [Link] deitelLinkLabel;
16 private [Link] notepadLinkLabel;
Deitel website
17 link
Notepad link
18 [STAThread]
19 static void Main()
20 {
21 [Link]( new LinkLabelTest() );
22 }
23
24 // browse C:\ drive
25 private void driveLinkLabel_LinkClicked( object sender,
26 [Link] e )
C drive
27 { event handler
28 [Link] = true;
29 [Link]( "C:\\" );
30 }
31 Start method to open
other programs
© 2002 Prentice Hall.
All rights reserved.
9
19
32 // load [Link] in Web broswer Outline
33 private void deitelLinkLabel_LinkClicked( object sender,
34 [Link] e )
35 {
36 [Link] = true; [Link]
Deitel website
37 [Link](
38 "IExplore", "[Link] );
event handler
39 }
40
41 // run application Notepad
42 private void notepadLinkLabel_LinkClicked(
43 object sender,
44 [Link] e ) Notepad
45 { event handler
46 [Link] = true;
47
48 // program called as if in run
49 // menu and full path not needed
50 [Link]( "notepad" );
51 }
52
53 } // end class LinkLabelTest
20
Outline
[Link]
Program Output
Click on first
LinkLabel to
look at contents
of C drive
10
21
Outline
[Link]
Program Output
Click on second
LinkLabel to go
to the Web Site
22
Outline
[Link]
Program Output
11
23
• ListBoxes
– Allow users to view and select from items on a list
– Static objects
– SelectionMode property determines number of items
that can be selected
– Property Items returns all objects in list
– Property SelectedItem returns current selected item
– Property SelectedIndex returns index of selected item
– Property GetSelected returns true if property at given
index is selected
– Use Add method to add to Items collection
• [Link](“myListItem”)
24
• CheckedListBoxes
– Extends ListBox by placing check boxes next to items
– Can select more than one object at one time
12
25
13.4.1 ListBoxes
• Class ListBoxTest
– Allows users to add and remove items from ListBox
– Uses event handlers to add to, remove from and clear list
26
13.4.2 CheckedListBoxes
13
27
ListBox
Checked item
CheckedListBox
28
14
29
30
1 // Fig 13.11: [Link] Outline
2 // Program to add, remove and clear list box items.
3
4 using System;
5 using [Link]; [Link]
6 using [Link];
7 using [Link];
8 using [Link];
9 using [Link];
10
11 public class ListBoxTest : [Link]
12 {
13 // contains user-input list of elements
14 private [Link] displayListBox; Display ListBox
15
16 // user input textbox Text field for input
17 private [Link] inputTextBox;
18
19 // add, remove, clear and exit command buttons
20 private [Link] addButton; Add button
21 private [Link] removeButton;
22 private [Link] clearButton; Clear button
Remove Button
23 private [Link] exitButton;
24
25 [STAThread] Exit button
26 static void Main()
27 {
28 [Link]( new ListBoxTest() );
29 }
30
15
31
31 // add new item (text from input box) Outline
32 // and clear input box
33 private void addButton_Click( Add event
34 object sender, [Link] e ) handler
35 { [Link]
36 [Link]( [Link] );
37 [Link]();
38 } Add method
39
40 // remove item if one selected
41 private void removeButton_Click(
42 object sender, [Link] e )
43 {
44 // remove only if item selected Test if item is
45 if ( [Link] != -1 )
46 [Link]( selected
Remove method
47 [Link] );
48 }
49
50 // clear all items
51 private void clearButton_Click(
52 object sender, [Link] e )
53 {
54 [Link]();
55 } Clear method
56
57 // exit application
58 private void exitButton_Click(
59 object sender, [Link] e )
60 {
61 [Link]();
Exit
62 }
63
64 } // end class ListBoxTest
© 2002 Prentice Hall.
All rights reserved.
32
Outline
[Link]
Program Output
16
33
CheckedListBox D e sc rip t io n / D e le g a t e a n d Ev e n t A rg u m e n t s
p ro p e rt ie s, m e t h o d s a n d
e ve nts
C o m m o n P r o p e r tie s (A ll th e L i s t B o x p ro p e r tie s a n d e v e n ts a r e in h e r ite d b y
C h e c k e d L i s t B o x .)
CheckedItems T h e co lle c tio n o f ite m s th a t a re ch e c k e d . N o t th e s a m e a s th e s e le c te d
ite m s , w h ich a re h ig h lig h te d (b u t n o t n e c e ss a ril y c h e c k ed ).
CheckedIndices R e tu rn s in d ic e s fo r th e ite m s th a t a re ch e c k e d . N o t th e s a m e a s th e
s e le c te d in d ic e s .
SelectionMode C a n o n ly h a v e v a lu e s O n e (a llo w s m u ltip le se le c tio n ) o r N o n e (d o e s
n o t a llo w m u ltip le s e le c tio n ).
C o m m o n M e th o d s
GetItemChecked T a k e s a n in d e x an d re tu rn s t r u e if c o rre sp o n d in g ite m c h e c k ed .
C o m m o n E v e n ts (D e le g a te I t e m C h e c k E v e n t H a n d l e r , e v e n t a r g u m e n ts
ItemCheckEventArgs)
ItemCheck R a is e d w h e n a n ite m is ch e c k ed o r u n c h e c k e d .
ItemCheckEventArgs
P r o p e r tie s
CurrentValue W h e th e r c u rren t ite m is c h e c k e d o r u n c h e c k e d . V a lu e s C h e c k e d ,
Unchecked or Indeterminate.
Index In d e x o f ite m th a t c h an g e d .
NewValue N e w s ta te o f ite m .
Fig . 1 3 .1 2 C h e c k e d L i s t B o x p ro p e rt ie s, m e t h o d s a n d e v e n t s.
34
1 // Fig. 13.13: [Link] Outline
2 // Using the checked list boxes to add items to a list box
3
4 using System;
5 using [Link]; CheckedListBoxTe
6 using [Link]; [Link]
7 using [Link];
8 using [Link];
9 using [Link];
10
11 public class CheckedListBoxTest : [Link]
12 {
13 // list of available book titles
14 private [Link] CheckedListBox
15 inputCheckedListBox;
16
17 // user selection list
18 private [Link] displayListBox;
19
ListBox
20 [STAThread]
21 static void Main()
22 {
23 [Link]( new CheckedListBoxTest() );
24 }
25
26 // item about to change,
27 // add or remove from displayListBox ItemCheck
28 private void inputCheckedListBox_ItemCheck(
29 object sender,
event handler
30 [Link] e )
31 {
32 // obtain reference of selected item
33 string item =
34 [Link]();
35 © 2002 Prentice Hall.
All rights reserved.
17
35
36 // if item checked add to listbox Outline
37 // otherwise remove from listbox
38 if ( [Link] == [Link] )
39 [Link]( item ); Add Item
40 else CheckedListBoxTe
41 [Link]( item ); [Link]
42 Remove Item
43 } // end method inputCheckedListBox_Click
44
45 } // end class CheckedListBox
Program Output
36
13.5 ComboBoxes
18
37
13.5 ComboBoxes
38
13.5 ComboBoxes
ComboBox e v e n ts a nd D e sc rip t io n / D e le g a t e a n d Ev e n t A rg u m e n t s
p ro p e rt ie s
Com m on Properties
DropDownStyle D eterm ines the type of com bo box. Value Simple m eans that the
text portion is editable and the list portion is always visible. Value
DropDown (the default) m eans that the text portion is editable but an
arrow button m ust be clicked to see the list portion. Value
DropDownList means that the text portion is not editable and the
arrow button m ust be clicked to see the list portion.
Items Collection of item s in the ComboBox control.
MaxDropDownItems M axim um number of item s to display in the drop-dow n list (betw een
1 and 100). If value is exceeded, a scroll bar appears.
SelectedIndex Returns index of currently selected item . If there is no currently
selected item , -1 is returned.
SelectedItem Returns reference to currently selected item.
Sorted If true, item s appear in alphabetical order. D efault false.
Comm on Events (D elegate EventHandler, event argum ents EventArgs)
SelectedIndexChanged Raised when selected index changes (i.e., a check box has been
checked or unchecked). Default w hen control double-clicked in
designer.
Fig . 13.15 ComboBox p ro p e rt ie s a n d e v e n ts.
19
39
1 // Fig. 13.16: [Link] Outline
2 // Using ComboBox to select shape to draw
3
4 using System;
5 using [Link]; [Link]
6 using [Link];
7 using [Link];
8 using [Link];
9 using [Link];
10
11 public class ComboBoxTest : [Link]
12 {
13 // contains shape list (circle, square, ellipse, pie) Create ComboBox
14 private [Link] imageComboBox;
15
16 [STAThread]
17 static void Main()
18 {
19 [Link]( new ComboBoxTest() );
20 }
21
22 // get selected index, draw shape SelectedIndexChanged
23 private void imageComboBox_SelectedIndexChanged( event handler
24 object sender, [Link] e )
25 {
26 // create graphics object, pen and brush
27 Graphics myGraphics = [Link](); Create graphics object
28
29 // create Pen using color DarkRed
30 Pen myPen = new Pen( [Link] );
31
Create pen
32 // create SolidBrush using color DarkRed
33 SolidBrush mySolidBrush =
34 new SolidBrush( [Link] );
Create brush
35 © 2002 Prentice Hall.
All rights reserved.
40
36 // clear drawing area setting it to color White Outline
37 [Link]( [Link] );
38
39 // find index, draw proper shape
40 switch ( [Link] ) Switch statement to [Link]
41 { determine correct object
42 case 0: // case circle is selected to draw
43 [Link](
44 myPen, 50, 50, 150, 150 );
45 break;
46 case 1: // case rectangle is selected
47 [Link](
48 myPen, 50, 50, 150, 150 );
49 break;
50 case 2: // case ellipse is selected Draw object
51 [Link](
52 myPen, 50, 85, 150, 115 );
53 break;
54 case 3: // case pie is selected
55 [Link](
56 myPen, 50, 50, 150, 150, 0, 45 );
57 break;
58 case 4: // case filled circle is selected
59 [Link](
60 mySolidBrush, 50, 50, 150, 150 );
61 break;
62 case 5: // case filled rectangle is selected
63 [Link](
64 mySolidBrush, 50, 50, 150, 150 );
65 break;
66 case 6: // case filled ellipse is selected
67 [Link](
68 mySolidBrush, 50, 85, 150, 115 );
69 break;
© 2002 Prentice Hall.
All rights reserved.
20
41
70 case 7: // case filled pie is selected Outline
71 [Link](
72 mySolidBrush, 50, 50, 150, 150, 0, 45 );
73 break;
74 [Link]
75 } // end switch
76
77 } // end method imageComboBox_SelectedIndexChanged
78
79 } // end class ComboBoxTest
Program Output
42
Outline
[Link]
Program Output
21
43
13.6 TreeViews
44
13.6 TreeView
Root node
22
45
13.6 TreeView
46
13.6 TreeView
TreeNode p ro p e rt ie s D e sc rip tio n / D e le g a t e a n d Ev e n t A rg u m e n t s
a nd m e tho d s
Common Properties
Checked Indicates w hether the TreeNode is checked.
( CheckBoxes property m ust be set to True in parent
TreeView .)
FirstNode Specifies the first node in the Nodes collection (i.e., first child
in tree).
FullPath Indicates the path of the node, starting at the root of the tree.
ImageIndex Specifies the index of the im age to be show n when the node is
deselected.
LastNode Specifies the last node in the Nodes collection (i.e., last child in
tree).
NextNode N ext sibling node.
Nodes The collection ofTreeNode s contained in the current node
Add
(i.e., all the children of the current node). Contains m ethods
(adds a TreeNode object), Clear (deletes the entire
collection) and Remove (deletes a specific node). Rem oving a
parent node deletes all its children.
PrevNode Indicates the previous sibling node.
SelectedImageI Specifies the index of the im age to use w hen the node is selected.
ndex
Text Specifies the text to display in the TreeView .
Common M ethods
Collapse Collapses a node.
Expand Expands a node.
ExpandAll Expands all the children of a node.
GetNodeCount Returns the number of child nodes.
Fig . 13.19 TreeNode p ro p e rt ie s a n d m e t h o d s.
23
47
13.6 TreeView
48
1 // Fig. 13.21: [Link] Outline
2 // Using TreeView to display directory structure
3
4 using System;
5 using [Link]; TreeViewDirector
6 using [Link]; yStructureTest.c
7 using [Link]; s
8 using [Link];
9 using [Link];
10 using [Link];
11
12 public class TreeViewDirectoryStructureTest
13 : [Link]
14 {
15 // contains view of c: drive directory structure
16 private [Link] directoryTreeView;
17
18 [STAThread]
19 static void Main()
20 {
21 [Link](
22 new TreeViewDirectoryStructureTest() );
23 }
24
25 public void PopulateTreeView( Class that creates
26 string directoryValue, TreeNode parentNode ) children of root
27 {
28 // populate current node with subdirectories
29 string[] directoryArray =
30 [Link]( directoryValue ); Get subdirectories
31 of root
24
49
32 // populate current node with subdirectories Outline
33 try
34 {
35 if ( [Link] != 0 )
36 { TreeViewDirector
37 // for every subdirectory, create new TreeNode, yStructureTest.c
38 // add as child of current node and recursively s
39 // populate child nodes with subdirectories
40 foreach ( string directory in directoryArray )
41 {
42 // create TreeNode for current directory
43 TreeNode myNode = new TreeNode( directory ); Create new node
44
45 // add current directory node to parent node
46 [Link]( myNode );
47
48 // recursively populate every subdirectory
49 PopulateTreeView( directory, myNode );
50 }
Recursive call
51 to finish tree
52 } // end if
53 }
54
55 // catch exception
56 catch ( UnauthorizedAccessException ) Catches security
57 { exception
58 [Link]( "Access denied" );
59 }
60
61 } // end PopulateTreeView
62
50
63 // called by system when form loads Outline
64 private void TreeViewDirectoryStructureTest_Load(
65 object sender, [Link] e)
66 {
67 // add c:\ drive to directoryTreeView and TreeViewDirector
68 // insert its subfolders yStructureTest.c
69 [Link]( "C:\\" ); Create root s
70 PopulateTreeView(
71 "C:\\", [Link][ 0 ] );
72 }
73
74 } // end class TreeViewDirectoryStructure
25
51
Outline
TreeViewDirector
yStructureTest.c
s
Program Output
52
13.7 ListViews
26
53
13.7 ListViews
ListV iew e v e n ts a nd D e sc rip t io n / D e le g a t e a n d Ev e n t A rg u m e n t s
p ro p e rt ie s
C om m on
P roperties
Ac t i v at i o n D eterm ines how the user activates an item . This property takes a value
in the I te m Ac t i v at io n enum eration. Possible values are
O n eC l i c k (single-click activation), T w o Cl ic k (double-
click activation, item changes color w hen selected) and
S t an d a r d (double-click activation).
Ch e c k Bo x e s T r ue displa ys
Indicates w hether item s appear w ith checkboxes.
F a l se .
checkboxes. D efault is
La r g e Im a g e Li s Indicates the I m a g eL i s t used w hen displayin g large icons.
t
It e m s R eturns the collection of Li s t V ie w I t em s in the control.
54
13.7 ListViews
27
55
1 // Fig. 13.24: [Link] Outline
2 // Displaying directories and their contents in ListView.
3
4 using System;
5 using [Link]; [Link]
6 using [Link];
7 using [Link];
8 using [Link];
9 using [Link];
10 using [Link];
11
12 public class ListViewTest : [Link]
13 {
14 // display labels for current location
15 // in directory tree
16 private [Link] currentLabel;
17 private [Link] displayLabel;
18
19 // display contents of current directory
20 private [Link] browserListView; Create Image List
21
22 // specifies images for file icons and folder icons
23 private [Link] fileFolder;
24
25 // get current directory
26 string currentDirectory = Load the current directory
27 [Link]();
28
29 [STAThread]
30 static void Main()
31 {
32 [Link]( new ListViewTest() );
33 }
34
© 2002 Prentice Hall.
All rights reserved.
56
35 // browse directory user clicked or go up one level Outline
36 private void browserListView_Click(
37 object sender, [Link] e )
38 {
39 // ensure item selected [Link]
40 if ( [Link] != 0 ) Test if item is
41 {
42 // if first item selected, go up one level selected
43 if ( [Link][ 0 ].Selected ) If first item selected
44 {
45 // create DirectoryInfo object for directory go up one level
46 DirectoryInfo directoryObject =
47 new DirectoryInfo( currentDirectory ); Make directory information
48
49 // if directory has parent, load it
50 if ( [Link] != null ) Test to see if at root
51 LoadFilesInDirectory(
52 [Link] );
53 }
Return parent of
54 current directory
55 // selected directory or file
56 else
57 {
58 // directory or file chosen
59 string chosen =
60 [Link][ 0 ].Text;
61
62 // if item selected is directory
63 if ( [Link]( currentDirectory +
64 "\\" + chosen ) ) Check if selected
65 { item is directory
28
57
66 // load subdirectory Outline
67 // if in c:\, do not need '\', Load subdirectory
68 // otherwise we do
69 if ( currentDirectory == "C:\\" )
70 LoadFilesInDirectory( [Link]
71 currentDirectory + chosen );
72 else
73 LoadFilesInDirectory(
74 currentDirectory + "\\" + chosen );
75 } //end if
76
77 } // end else
78
79 // update displayLabel
80 [Link] = currentDirectory; Update to display
81
82 } // end if
current directory
83
84 } // end method browserListView_Click
85
86 // display files/subdirectories of current directory
87 public void LoadFilesInDirectory(
88 string currentDirectoryValue ) Class to load files in
89 { current directory
90 // load directory information and display
91 try
92 {
93 // clear ListView and set first item
94 [Link]();
95 [Link]( "Go Up One Level" );
96
58
97 // update current directory Outline
98 currentDirectory = currentDirectoryValue;
99 DirectoryInfo newCurrentDirectory =
100 new DirectoryInfo( currentDirectory );
101 [Link]
102 // put files and directories into arrays
103 DirectoryInfo[] directoryArray =
104 [Link](); Get subdirectories of
105 current directory
106 FileInfo[] fileArray =
107 [Link]();
Get files of
108 current directory
109 // add directory names to ListView
110 foreach ( DirectoryInfo dir in directoryArray )
111 {
112 // add directory to ListView
113 ListViewItem newDirectoryItem =
114 [Link]( [Link] ); Add directory to list
115
116 // set directory image
117 [Link] = 0;
118 }
119
120 // add file names to ListView
121 foreach ( FileInfo file in fileArray ) Add file to list
122 {
123 // add file to ListView
124 ListViewItem newFileItem =
125 [Link]( [Link] );
126
127 [Link] = 1; // set file image
128 }
129 } // end try
130
© 2002 Prentice Hall.
All rights reserved.
29
59
131 // access denied Outline
132 catch ( UnauthorizedAccessException exception )
133 {
134 [Link](
Security exception
135 "Warning: Some fields may not be " + handler [Link]
136 "visible due to permission settings",
137 "Attention", 0, [Link] );
138 }
139
140 } // end method LoadFilesInDirectory
141
142 // handle load event when Form displayed for first time
143 private void ListViewTest_Load(
144 object sender, [Link] e )
145 {
146 // set image list
147 Image folderImage = [Link](
148 currentDirectory + "\\images\\[Link]" ); Load Images
149
150 Image fileImage = [Link]( currentDirectory +
151 "\\images\\[Link]" );
152
153 [Link]( folderImage );
154 [Link]( fileImage );
155
156 // load current directory into browserListView
157 LoadFilesInDirectory( currentDirectory );
158 [Link] = currentDirectory;
159
160 } // end method ListViewTest_Load
161
162 } // end class ListViewTest
60
Outline
[Link]
Program Output
30
61
13.8 TabControl
62
Tab pages
13.8 Tab Controls
31
63
TabPage
TabControl
Controls in
TabPage
64
32
65
66
1 // Fig. 13.29: [Link] Outline
2 // Using TabControl to display various font settings.
3
4 using System;
5 using [Link]; [Link]
6 using [Link];
7 using [Link];
8 using [Link];
9 using [Link];
10
11 public class UsingTabs : [Link]
12 {
13 // output label reflects text changes
14 private [Link] displayLabel;
15
16 // table control containing table pages colorTabPage,
17 // sizeTabPage, messageTabPage and aboutTabPage
18 private [Link]
19 optionsTabControl;
20
21 // table page containing color options Color tab
22 private [Link] colorTabPage;
23 private [Link]
24 greenRadioButton;
25 private [Link] redRadioButton; Color buttons for
26 private [Link]
27 blackRadioButton; color tab
28
33
67
29 // table page containing font size options
Size tab
Outline
30 private [Link] sizeTabPage;
31 private [Link]
32 size20RadioButton;
33 private [Link] Size buttons [Link]
34 size16RadioButton;
35 private [Link]
36 size12RadioButton;
37
38 // table page containing text display options
39 private [Link] messageTabPage; Message tab
40 private [Link]
41 goodByeRadioButton;
42 private [Link]
43 helloRadioButton;
44
45 // table page containing about message
46 private [Link] aboutTabPage;
47 private [Link] messageLabel;
About tab
48
49 [STAThread]
50 static void Main()
51 {
52 [Link]( new UsingTabs() );
53 }
54 Event handler
55 // event handler for black color radio button
56 private void blackRadioButton_CheckedChanged(
57 object sender, [Link] e )
58 {
59 [Link] = [Link];
60 }
61
© 2002 Prentice Hall.
All rights reserved.
68
62 // event handler for red color radio button Outline
63 private void redRadioButton_CheckedChanged(
64 object sender, [Link] e )
65 {
66 [Link] = [Link]; [Link]
67 }
68
69 // event handler for green color radio button
70 private void greenRadioButton_CheckedChanged(
71 object sender, [Link] e )
72 {
73 [Link] = [Link];
74 }
75 Event handlers
76 // event handler for size 12 radio button
77 private void size12RadioButton_CheckedChanged(
78 object sender, [Link] e )
79 {
80 [Link] =
81 new Font( [Link], 12 );
82 }
83
84 // event handler for size 16 radio button
85 private void size16RadioButton_CheckedChanged(
86 object sender, [Link] e )
87 {
88 [Link] =
89 new Font( [Link], 16 );
90 }
91
34
69
92 // event handler for size 20 radio button Outline
93 private void size20RadioButton_CheckedChanged(
94 object sender, [Link] e )
95 {
96 [Link] = [Link]
97 new Font( [Link], 20 );
98 }
99
100 // event handler for message "Hello!" radio button
101 private void helloRadioButton_CheckedChanged(
102 object sender, [Link] e ) Event handlers
103 {
104 [Link] = "Hello!";
105 }
106
107 // event handler for message "Goodbye!" radio button
108 private void goodByeRadioButton_CheckedChanged(
109 object sender, [Link] e )
110 {
111 [Link] = "Goodbye!";
112 }
113
114 } // end class UsingTabs
70
Outline
[Link]
Program Output
35
71
72
13.9 Multiple Document Interface (MDI)
Windows
MDI parent
MDI child
MDI child
36
73
13.9 Multiple Document Interface (MDI)
Windows
74
13.9 Multiple Document Interface (MDI)
Windows
M DI Form e v e nt s a n d De sc rip tio n / De le g a te a n d Ev e n t A rg u m e nt s
p ro p e rtie s
Common MDI Child
Properties
IsMdiChild Indicates whether the Form is an MDI child. If True ,
Form is an MDI child (read-only property).
MdiParent Specifies the MDI parent Form of the child.
Common MDI Parent
Properties
ActiveMdiChild Returns theForm that is the currently active MDI child
(returnsnull if no children are active).
IsMdiContainer Indicates whether a Form can be an MDI. If True , the
Form can be an MDI parent. Default is False .
MdiChildren Returns the MDI children as an array of Form s.
Common Method
LayoutMdi Determines the display of child forms on an MDI parent. Takes
as a parameter an MdiLayout enumeration with possible
ArrangeIcons , Cascade ,
values
TileHorizontal and TileVertical .
Figure 13.35 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.32 M D I p a re n t a n d M D I c h ild e v e n ts a n d p ro p e rtie s.
37
75
13.9 Multiple Document Interface (MDI)
Windows
Parent’s icons: minimize, Maximized child’s icons: minimize,
maximize and close restore and close
76
13.9 Multiple Document Interface (MDI)
Windows
38
77
13.9 Multiple Document Interface (MDI)
Windows
ArrangeIcons Cascade
78
13.9 Multiple Document Interface (MDI)
Windows
TileHorizontal TileVertical
39
79
1 // Fig. 13.36: [Link] Outline
2 // Demonstrating use of MDI parent and child windows.
3 using System;
4 using [Link];
5 using [Link]; [Link]
6 using [Link];
7 using [Link];
8 using [Link];
9
10 public class UsingMDI : [Link]
11 {
12 private [Link] mainMenu1; File menu
13 private [Link] fileMenuItem;
14 private [Link] newMenuItem;
15 private [Link] child1MenuItem; New submenu
16 private [Link] child2MenuItem;
17 private [Link] child3MenuItem;
18 private [Link] exitMenuItem; Exit submenu
19 private [Link] formatMenuItem; Formant menu
20 private [Link] cascadeMenuItem;
21 private [Link] Cascade option
22 tileHorizontalMenuItem;
23 private [Link]
24 tileVerticalMenuItem;
25 Tiling options
26 [STAThread]
27 static void Main()
28 {
29 [Link]( new UsingMDI() );
30 }
31
80
32 // create Child 1 when menu clicked Outline
33 private void child1MenuItem_Click(
34 object sender, [Link] e )
35 {
36 // create new child [Link]
37 Child formChild = new Child( "Child 1",
38 "\\images\\[Link]" );
39 [Link] = this; // set parent
40 [Link](); // display child
41 }
42
43 // create Child 2 when menu clicked
44 private void child2MenuItem_Click(
45 object sender, [Link] e )
46 { Create child windows
47 // create new child
48 Child formChild = new Child( "Child 2",
49 "\\images\\[Link]" );
50 [Link] = this; // set parent
51 [Link](); // display child
52 }
53
54 // create Child 3 when menu clicked
55 private void child3MenuItem_Click(
56 object sender, [Link] e )
57 {
58 // create new child
59 Child formChild = new Child( "Child 3",
60 "\\images\\[Link]" );
61 [Link] = this; // set parent
62 [Link](); // display child
63 }
64
© 2002 Prentice Hall.
All rights reserved.
40
81
65 // exit application Outline
66 private void exitMenuItem_Click(
67 object sender, [Link] e )
68 {
69 [Link](); [Link]
70 }
71
72 // set cascade layout
73 private void cascadeMenuItem_Click( Cascade
74 object sender, [Link] e )
75 {
76 [Link]( [Link] );
77 }
78
79 // set TileHorizontal layout
80 private void tileHorizontalMenuItem_Click(
Tile horizontally
81 object sender, [Link] e )
82 {
83 [Link]( [Link] );
84 }
85
86 // set TileVertical layout
87 private void tileVerticalMenuItem_Click( Tile vertically
88 object sender, [Link] e )
89 {
90 [Link]( [Link] );
91 }
92
93 } // end class UsingMDI
82
Outline
[Link]
Program Output
41
83
1 // Fig. 13.37: [Link] Outline
2 // Child window of MDI parent.
3 using System;
4 using [Link];
5 using [Link]; [Link]
6 using [Link]; Child class
7 using [Link]; Create picture box
8 using [Link];
9
10 public class Child : [Link]
11 {
12 private [Link] pictureBox;
13
14 public Child( string title, string fileName )
15 {
16 // Required for Windows Form Designer support
17 InitializeComponent();
18
19 Text = title; // set title text Display title
20
21 // set image to display in pictureBox
22 [Link] = [Link](
23 [Link]() + fileName );
24 }
25 } Display picture
84
42
85
1 // Fig. 13.38: [Link] Outline
2 // Base Form for use with visual inheritance
3 using System;
4 using [Link];
5 using [Link]; VisualInheritanc
6 using [Link]; [Link]
7 using [Link];
8 using [Link];
9
10 public class VisualInheritance : [Link]
11 {
12 private [Link] bugsLabel;
13 private [Link] learnMoreButton;
14 private [Link] label1;
15
16 [STAThread]
17 static void Main()
18 {
19 [Link]( new VisualInheritance() );
20 }
21
22 private void learnMoreButton_Click( object sender,
23 [Link] e )
24 {
Learn More
25 [Link]( display method
26 "Bugs, Bugs, Bugs is a product of [Link]",
27 "Learn More", [Link],
28 [Link] );
29 }
30 }
86
Outline
VisualInheritanc
[Link]
Program Output
43
87
88
1 // Fig. 13.40: [Link] Outline
2 // Derived Form using visual inheritance.
3 using System;
4 using [Link];
5 using [Link];
VisualInheritanc
6 using [Link]; [Link]
7 using [Link];
8
9 public class VisualInheritanceTest : VisualInheritanceTest class is derived
10 [Link] from VisualInheritance class
11 {
12 private [Link] learnProgramButton;
13
14 // invoke when user clicks Learn the Program Button
15 private void learnProgramButton_Click( object sender,
16 [Link] e )
Display message box
17 {
18 [Link](
19 "This program was created by Deitel & Associates",
20 "Learn the Program", [Link],
21 [Link] );
22 }
23
24 public static void Main( string[] args )
25 {
26 [Link]( new VisualInheritanceTest() );
27 }
28 }
44
89
Outline
VisualInheritanc
[Link]
Program Output
Derived class
cannot modify Derived class can
these controls modify this control
90
45
91
92
1 // Fig. 13.42: [Link] Outline
2 // User-defined control with a timer and a label.
3
4 using System;
5 using [Link]; ClockUserControl
6 using [Link]; .cs
7 using [Link];
8 using [Link];
9 using [Link];
10
11 public class ClockUserControl
12 : [Link]
13 {
14 private [Link] clockTimer;
Timer
15 private [Link] displayLabel;
16 Label
17 // update label at every tick
18 private void clockTimer_Tick( Update label method
19 object sender, [Link] e )
20 {
21 // get current time (Now), convert to string
22 [Link] = [Link](); Display current time
23
24 } // end method clockTimer_Tick
25
26 } // end class ClockUserControl
46
93
94
47
95
96
48