0% found this document useful (0 votes)
4 views40 pages

Lecture 7 V2

The document provides an overview of menus and TreeView controls in Windows applications, detailing their properties, methods, and examples of usage. It explains how to create menus with commands, submenus, and shortcut keys, as well as how to structure and manipulate TreeViews to display hierarchical data. Additionally, it includes code examples demonstrating how to implement these features in a Windows Forms application.

Uploaded by

Ahmed Al-nasheri
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views40 pages

Lecture 7 V2

The document provides an overview of menus and TreeView controls in Windows applications, detailing their properties, methods, and examples of usage. It explains how to create menus with commands, submenus, and shortcut keys, as well as how to structure and manipulate TreeViews to display hierarchical data. Additionally, it includes code examples demonstrating how to implement these features in a Windows Forms application.

Uploaded by

Ahmed Al-nasheri
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 40

Dr.

Ahmed Alnasheri
Contents
Menus
 Menu Example
TreeView
 TreeView Example

2
Menus
 Groups of related commands for Windows applications

 Integral part of GUIs

 Organize commands without “cluttering” the GUI

 All menu items can have access shortcuts or hotkeys

 Type an ampersand (&) before the character to be underlined


 Set the ShortcutKeys property of the appropriate
ToolStripMenuItems

 Non-top-level menus can also have shortcut keys

 Menu items can display check marks

 Indicates that multiple options on the menu can be selected at once


3
Menus
Submenu Shortcut key
Menu Menu items
Disabled command

Checked menu item

Seperator bar
4
Menus
 The type here text allows new items to be added

 When you click on type here a pull down appears letting


you select
 Menu item

 ComboBox

 Separator

 TextBox

5
Menus - MenuStrip
 You can add Menus to a form, with several menu items
that act like a commands a user can click to perform
certain actions.

MenuStrip

Menu Item

Separator Line

6
Sub Menus

Sub Menu

7
MenuStrip - Properties
 RightToLeft: Determines the direction of a
menustrip.

RightToLeft: No

RightToLeft: Yes

8
MenuItem - Properties
 Text: The text displayed in a menu item. When you
add ‘&’ before a certain letter. This letter will appear
underlined, and you can display its menu using (Alt +
the letter). (Note: & is used only for the main menu
items).

9
MenuItem - Properties
 ShortcutKeys: Determines the keys used as a shortcut
to perform the action using keyboard instead of a
mouse click.

 ShowShortcutKeys: of type bool; when true, the


shortcut keys will appear next to the menu item, as a
hint for the user.

10
Look-and-Feel Observation
 Buttons can have access shortcuts. Place the &
symbol immediately before the desired character
in the Button’s label. To press the button by using
its access key in the running application, the user
presses Alt and the underlined character.

11
MenuItem - Properties
 Checked: of type bool; if true, a check will appear
next to the menuitem (it will behave as a checkbox)

 CheckOnClick: of type bool; if true, then the


menuitem will be automatically checked or unchecked
using the mouse.
Checked= false

 CheckState – one of
 CheckState.Checked
Checked= true
 CheckState.Unchecked
 CheckState.Indeterminate

12
MenuItem – Default Event
 Click: Occurs when the user clicks on the menu item
with a mouse, or by using the shortcut keys.

13
Look-and-Feel Observation
 It is a convention to place an ellipsis ( ) after the
name of a menu item that when selected, displays
a dialog (e.g. Save As...). Menu items that produce
an immediate action without prompting the user
for more information (e.g. Save) should not have
an ellipsis following their name.

14
Menus Example

15
Menus Example
1 // Fig. 14.7: MenuTestForm.cs
2 // Using Menus to change font colors and styles.
3 using System;
4 using System.Drawing;
5 using System.Windows.Forms;
6
7 // our Form contains a Menu that changes the font color
8 // and style of the text displayed in Label
9 public partial class MenuTestForm : Form
10 {
11 // default constructor
12 public MenuTestForm()
13 {
14 InitializeComponent();
15 } // end constructor
16
17 // display MessageBox when About ToolStripMenuItem is selected
18 private void aboutToolStripMenuItem_Click( object sender, EventArgs e )
19 {
20 MessageBox.Show(
21 "This is an example\nof using menus.",
22 "About", MessageBoxButtons.OK, MessageBoxIcon.Information );
23 } // end method aboutToolStripMenuItem_Click
24
25 // exit program when Exit ToolStripMenuItem is selected
26 private void exitToolStripMenuItem_Click( object sender, EventArgs e )
27 {
28 Application.Exit();
29 } // end method exitToolStripMenuItem_Click

16
Menus Example
30
31 // reset checkmarks for Color ToolStripMenuItems
32 private void ClearColor()
33 {
34 // clear all checkmarks
35 blackToolStripMenuItem.Checked = false;
36 blueToolStripMenuItem.Checked = false;
37 redToolStripMenuItem.Checked = false;
38 greenToolStripMenuItem.Checked = false;
39 } // end method ClearColor
40
41 // update Menu state and color display black
42 private void blackToolStripMenuItem_Click( object sender, EventArgs e )
43 {
44 // reset checkmarks for Color ToolStripMenuItems
45 ClearColor();
46
47 // set Color to Black
48 displayLabel.ForeColor = Color.Black;
49 blackToolStripMenuItem.Checked = true;
50 } // end method blackToolStripMenuItem_Click

17
Menus Example
51
52 // update Menu state and color display blue
53 private void blueToolStripMenuItem_Click( object sender, EventArgs e )
54 {
55 // reset checkmarks for Color ToolStripMenuItems
56 ClearColor();
57
58 // set Color to Blue
59 displayLabel.ForeColor = Color.Blue;
60 blueToolStripMenuItem.Checked = true;
61 } // end method blueToolStripMenuItem_Click
62
63 // update Menu state and color display red
64 private void redToolStripMenuItem_Click( object sender, EventArgs e )
65 {
66 // reset checkmarks for Color ToolStripMenuItems
67 ClearColor();
68
69 // set Color to Red
70 displayLabel.ForeColor = Color.Red;
71 redToolStripMenuItem.Checked = true;
72 } // end method redToolStripMenuItem_Click

18
Menus Example
73
74 // update Menu state and color display green
75 private void greenToolStripMenuItem_Click( object sender, EventArgs e )
76 {
77 // reset checkmarks for Color ToolStripMenuItems
78 ClearColor();
79
80 // set Color to Green
81 displayLabel.ForeColor = Color.Green;
82 greenToolStripMenuItem.Checked = true;
83 } // end method greenToolStripMenuItem_Click
84
85 // reset checkmarks for Font ToolStripMenuItems
86 private void ClearFont()
87 {
88 // clear all checkmarks
89 timesToolStripMenuItem.Checked = false;
90 courierToolStripMenuItem.Checked = false;
91 comicToolStripMenuItem.Checked = false;
92 } // end method ClearFont

19
Menus Example
93
94 // update Menu state and set Font to Times New Roman
95 private void timesToolStripMenuItem_Click( object sender, EventArgs e )
96 {
97 // reset checkmarks for Font ToolStripMenuItems
98 ClearFont();
99
100 // set Times New Roman font
101 timesToolStripMenuItem.Checked = true;
102 displayLabel.Font = new Font(
103 "Times New Roman", 14, displayLabel.Font.Style );
104 } // end method timesToolStripMenuItem_Click
105
106 // update Menu state and set Font to Courier
107 private void courierToolStripMenuItem_Click(
108 object sender, EventArgs e )
109 {
110 // reset checkmarks for Font ToolStripMenuItems
111 ClearFont();
112
113 // set Courier font
114 courierToolStripMenuItem.Checked = true;
115 displayLabel.Font = new Font(
116 "Courier", 14, displayLabel.Font.Style );
117 } // end method courierToolStripMenuItem_Click
20
Menus Example
118
119 // update Menu state and set Font to Comic Sans MS
120 private void comicToolStripMenuItem_Click( object sender, EventArgs e )
121 {
122 // reset checkmarks for Font ToolStripMenuItems
123 ClearFont();
124
125 // set Comic Sans font
126 comicToolStripMenuItem.Checked = true;
127 displayLabel.Font = new Font(
128 "Comic Sans MS", 14, displayLabel.Font.Style );
129 } // end method comicToolStripMenuItem_Click
130
131 // toggle checkmark and toggle bold style
132 private void boldToolStripMenuItem_Click( object sender, EventArgs e )
133 {
134 // toggle checkmark
135 boldToolStripMenuItem.Checked = !boldToolStripMenuItem.Checked;
136
137 // use logical exlusive OR to toggle bold, keep all other styles
138 displayLabel.Font = new Font(
139 displayLabel.Font.FontFamily, 14,
140 displayLabel.Font.Style ^ FontStyle.Bold );
141 } // end method boldToolStripMenuItem_Click
21
Menus Example
142
143 // toggle checkmark and toggle italic style
144 private void italicToolStripMenuItem_Click(
145 object sender, EventArgs e )
146 {
147 // toggle checkmark
148 italicToolStripMenuItem.Checked = !italicToolStripMenuItem.Checked;
149
150 // use logical exclusive OR to toggle italic, keep all other styles
151 displayLabel.Font = new Font(
152 displayLabel.Font.FontFamily, 14,
153 displayLabel.Font.Style ^ FontStyle.Italic );
154 } // end method italicToolStripMenuItem_Click
155 } // end class MenuTestForm

22
Software Engineering Observation
 The mutual exclusion of menu items is not enforced
by the MenuStrip, even when the Checked property
is true. You must program this behavior.

 Color options mutually exclusive

23
TreeViews
 A Treeview is a control used to display items in a tree style,
that looks like the folders in Windows Explorer.
 Item in a treeview is called Node.
 Trees consists of several levels.
 Terms used in a tree structure:
 Root: A node in the first level, that doesn’t have a parent.
 Parent: A node that has child nodes.
 Child: A node that has a parent node.
 Siblings: Nodes in the same level, and have the same parent.
 Leaf: A node in the last level of the tree, and don’t have child
nodes.

24
TreeView – Design Time
 To add nodes, go to Nodes Properties
or Edit Nodes in the treeview itself.
• In this dialog you can add the tree nodes either
in the root level, or as children.

25
TreeView - Properties
 Nodes: array of nodes in a certain level.
 treeView1.Nodes: is an array of nodes in the first level
(root).
 treeView1.Nodes[0].Nodes: an array of nodes that
are children of the first node in the root level.
 treeView1.SelectedNode.Nodes: an array of nodes
that are children of the selectednode.
 SelectedNode: The current selected node in the tree,
no matter in which level it is.

26
TreeView - Properties
 CheckBoxes: boolean property on the treeview level,
when true, the tree will look like in the following
image.

 Checked: boolean property on the node level, when it


is true, means that the node is checked by the user, or
by default.

27
TreeView/TreeNode - Properties
 Name: The name of a node (as an object)
 Text: the string that the node is displayed by in the tree.
 treeView1.SelectedNode.Parent: Retrieves the node in
the upper level of the selectednode (its parent).
 treeView1.SelectedNode.FirstNode: Returns the first
child in the selectednode children.
 treeView1.SelectedNode.LastNode: Returns the first
child in the selectednode children.
 treeView1.SelectedNode.NextNode: Returns the next
sibling of the selected node.
 treeView1.SelectedNode.PrevNode: Returns the
Previous sibling of the selected node.

28
TreeView/TreeNode - Properties
 treeView1.SelectedNode.FullPath: Returns the full
path of the selected node, starting from the root node.
 treeView1.Nodes.Count: Number of nodes in the
root.
 treeView1.SelectedNode.Nodes.Count: Number of
children of the selected node.
 treeView1.SelectedNode.Tag: a user defined
property that you can add any type of data in it and to
be associated to a tree node.

29
TreeView – Default Event
 AfterSelect: Occurs after a node is selected in the
TreeView

30
TreeView - Methods
 treeView1.Nodes.Add(…): Adds a node as a root.
 treeView1.SelectedNode.Nodes.Add(): Adds a node
as a child to the selected node.
 treeView1.SelectedNode.GetNodeCount(false):
Returns number of children of the selected node, in
the direct level only.
 treeView1.SelectedNode.GetNodeCount(true):
Returns number of children of the selected node, in all
levels.

31
TreeView - Methods
 treeView1.ExpandAll(): Expands all children in all
levels in the whole tree.
 treeView1.SelectedNode.Expand(): Expands
children of the selected node in the direct level.
 treeView1.SelectedNode.ExpandAll(): Expands
children of the selected node in all levels.
 treeView1.SelectedNode.Collapse(): Hides the
children of the selected node.
 treeView1.CollapseAll(): Hides all children in all
levels in the tree.
32
TreeView Example

33
TreeView Example
1 // Fig. 14.28: TreeViewDirectoryStructureForm.cs
2 // Using TreeView to display directory structure.
3 using System;
4 using System.Windows.Forms;
5 using System.IO;
6
7 // Form uses TreeView to display directory structure
8 public partial class TreeViewDirectoryStructureForm : Form
9 {
10 string substringDirectory; // store last part of full path name
11
12 // default constructor
13 public TreeViewDirectoryStructureForm()
14 {
15 InitializeComponent();
16 } // end constructor
17
18 // populate current node with subdirectories
19 public void PopulateTreeView(
20 string directoryValue, TreeNode parentNode )
21 {
22 // array stores all subdirectories in the directory
23 string[] directoryArray =
24 Directory.GetDirectories( directoryValue );
34
TreeView Example
25
26 // populate current node with subdirectories
27 try
28 {
29 // check to see if any subdirectories are present
30 if ( directoryArray.Length != 0 )
31 {
32 // for every subdirectory, create new TreeNode,
33 // add as a child of current node and recursively
34 // populate child nodes with subdirectories
35 foreach ( string directory in directoryArray )
36 {
37 // obtain last part of path name from the full path name
38 // by finding the last occurence of "\" and returning the
39 // part of the path name that comes after this occurence
40 substringDirectory = directory.Substring(
41 directory.LastIndexOf( '\\' ) + 1,
42 directory.Length - directory.LastIndexOf( '\\' ) - 1 );
43
44 // create TreeNode for current directory
45 TreeNode myNode = new TreeNode( substringDirectory );
46
47 // add current directory node to parent node
48 parentNode.Nodes.Add( myNode );
49
50 // recursively populate every subdirectory
51 PopulateTreeView( directory, myNode );
52 } // end foreach
53 } // end if
54 } //end try 35
TreeView Example
55
56 // catch exception
57 catch ( UnauthorizedAccessException )
58 {
59 parentNode.Nodes.Add( "Access denied" );
60 } // end catch
61 } // end method PopulateTreeView
62
63 // handles enterButton click event
64 private void enterButton_Click( object sender, EventArgs e )
65 {
66 // clear all nodes
67 directoryTreeView.Nodes.Clear();
68
69 // check if the directory entered by user exists
70 // if it does then fill in the TreeView,
71 // if not display error MessageBox
72 if ( Directory.Exists( inputTextBox.Text ) )
73 {
74 // add full path name to directoryTreeView
75 directoryTreeView.Nodes.Add( inputTextBox.Text );
76
77 // insert subfolders
78 PopulateTreeView(
79 inputTextBox.Text, directoryTreeView.Nodes[ 0 ] );
80 }
36
Homework 5 - A
 Create Application form that do the following:

37
Homework 5 - A
 Create Application form that do the following:

38
Homework 5 - B

39
The End
40

You might also like