Week 3
Windows Controls
Contents
MenuStrip and ContextMenuStrip control
TreeView (p.110)
Advanced topics (p.521)
Slide 2
Create MenuStrip at run-time
Add a menu to MenuStrip control at run-time
MenuStrip.Items.Add method
Example:
MenuStrip.Items.Add("File");
MenuStrip.Items.Add("View");
Add a menu item to other menu item at run-time
ToolStripMenuItem.DropDownItems.Add method
Example:
ToolStripItem mnuOpen;
mnuOpen = fileMenuItem.DropDownItems.Add("Open");
mnuOpen.Click += new EventHandler(mnuOpen_Click);
Slide 3
ContextMenuStrip control (p.173)
ContextMenuStrip similar to the MenuStrip control
The main difference between the ContextMenuStrip
and the MenuStrip
ContextMenuStrip control does not have a top-level
menu
Not visible at run time unless invoked by right-clicking
the control that it is associated with
Slide 4
ContextMenuStrip control (cont.)
Associating a ContextMenuStrip property with a
control
Using ContextMenuStrip property of a control or
Write in Mouse_Up event handler
ContextMenuStrip1.Show(Control, X, Y)
Demo:
Module 4 – Exercise 3 (modified)
Slide 5
TreeView control (p.110)
The TreeView control
allows you to display a
list of objects in a
hierarchical manner
Node
Root
Parent Node
Child Node
Level of nodes
TreeNode object
Slide 11
TreeView properties
Nodes: gets the collection of child nodes belong to
TreeView
FullRowSelect: True/False
HideSelection: True/False
ImageList
LabelEdit: True/False
ShowLines: True/False
SelectedNode: get or set the selected node
Slide 12
Add icons for TreeNode
Using ImageList like ListView
For each TreeNode, set:
ImageIndex
SelectedImageIndex
Exercise: Module 5 - Ex 1,2
Slide 15
TreeNode properties
Text
ImageIndex
SelectedImageIndex
IsSelected
Level
Parent
Nodes: gets the collection of child nodes belong to a
TreeNode
Slide 16
TreeNode methods
Methods Description
Expand() Expands a node
Collapse() Collapses a node
ExpandAll() Expands all the children of a node
CollapseAll() Collapses all the children of a node
GetNodeCount() Return the number of child node
Slide 17
Methods, properties of TreeView,
TreeNode
Some important methods, properties of the TreeView,
TreeNode:
.Nodes.Add( string text )
.Nodes.Add( TreeNode node )
.Nodes.Remove( TreeNode node )
.Nodes.RemoveAt( int index )
.Nodes.Clear()
.Nodes.Count
.Nodes[int index]: returns a TreeNode object
Slide 18
Default event of TreeView
AfterSelect event: occurs when the selection has
been changed
private void treeview1_AfterSelect(…)
{
if (treeview1.SelectedNode != null)
{
// process selected node
}
}
Slide 19
Operations on TreeView control
Add node
Delete selected node
Display selected node
Visit nodes
Search and select node
Expanding and collapsing nodes
Level of TreeNode
Using Tag property of TreeNode
Slide 20
Add node
To add root node to TreeView:
TreeView1.Nodes.Add(…)
To add a node to the root node:
TreeView1.Nodes[0].Nodes.Add(…)
To add a node to the second child node of root node:
TreeView1.Nodes[0].Nodes[1].Nodes.Add(…)
Rule to add node:
1. Get the parent node
2. Add a node to that parent node
Slide 21
Add node (cont.)
Example 1: Add a node to selected node
Example 2: Create tree
TreeNode root;
root = treeView1.Nodes.Add("Công ty IBM");
root.Nodes.Add("Phòng tiếp thị");
root.Nodes[0].Nodes.Add("Lam Trường");
TreeNode parent = TreeView1.SelectedNode;
parent.Nodes.Add(TextBox1.Text);
Slide 22
Delete selected node
private void DeleteSelectedNode(TreeView tree)
{
if (tree.SelectedNode != null)
tree.Nodes.Remove(tree.SelectedNode);
}
Slide 23
Display selected node
private void treeview1_AfterSelect(…)
{
if (treeview1.SelectedNode != null)
{
TextBox1.Text = treeview1.SelectedNode.Text;
//
}
}
Slide 24
Visit direct children of the node
Visitting direct children of selected node, by:
You can visit direct children of any node
foreach (TreeNode node in tree.SelectedNode.Nodes)
{
// process node
}
Slide 25
Search and select node
Set HideSelection property is false
Loop all node using node visiting
Note EnsureVisible property for selected TreeNode
foreach (TreeNode node in SearchNode.Nodes)
{
if (match)
{
treeView1.SelectedNode = node;
}
}
Slide 26
Expanding and collapsing nodes
Expand and collapse methods of node
Use Expand or Collapse method of TreeView,
TreeNode
Expand all node
Using recursion
Treeview1.ExpandAll();
Example
Write program to expand - collapse all node in Module
5 exercise 1
Slide 27
Level of TreeNode
Use Level property to process the node you want
Slide 28
Using Tag property of TreeNode
Using Tag property to store information you want
Set Tag property for each TreeNode
Slide 29
Advanced topics in Windows Forms
MDI application (p.543)
Drag-Drop (p.523)
Self-study
Using user assistance controls and components
Implementing Globalization and Localization for a
Windows Forms application
Deploying applications with ClickOnce
Creating setup projects for deployment
Slide 32
Using user assistance controls
and components
PropertyGrid
ProgressBar
StatusStrip
ToolTip
ErrorProvider
HelpProvider
SoundPlayer
Timer
HScrollBar
VScrollBar
Slide 33
MDI application
Multiple Document Interface (MDI) Windows
Single Document Interface (SDI) Multiple Document Interface (MDI)
IsMdiContainer = False
IsMdiContainer = True
Slide 34