Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Лаба №1 / books / csharp_ebook.pdf
Скачиваний:
77
Добавлен:
03.03.2016
Размер:
3.69 Mб
Скачать

Programmers Heaven: C# School

The TreeNode Editor

The easiest way to add and remove items to and from the tree view control is through its 'Nodes' property in the form designer. When you click the Nodes property in the properties windows of the form designer, it will show the following screen, which is known as the Tree Node Editor.

215

Programmers Heaven: C# School

'Add Root' will add a root element (one with no parent e.g., World in the above window). 'Add Child' will add a child node to the selected element. The label of the nodes can be changed using the text box labeled 'Label'. A node can be deleted using the 'Delete' button.

Adding/Removing items at runtime

Items can be added to or removed from the tree view using the TreeNode editor at design time, but most of the time we need to add/remove items at runtime using our code. The TreeView class contains a property called 'Nodes' which provides access to the individual nodes of the control. The Add() method of the Nodes collection can be used to add a text item or a TreeNode object which itself may have child tree nodes. The following code adds a sample tree hierarchy on the form's Load event.

private void MyForm_Load(object sender, System.EventArgs e)

{

treeView1.Nodes.Clear();

treeView1.Nodes.Add("Programming Languages");

TreeNode node = new TreeNode("Object Oriented Programming Languages");

node.Nodes.Add("C++");

TreeNode subnode = node.Nodes.Add("Framework and runtime based languages");

subnode.Nodes.Add("Java");

216

Programmers Heaven: C# School

subnode.Nodes.Add("C#");

treeView1.Nodes[0].Nodes.Add(node);

}

First we have added a root node labeled 'Programming Languages'. We then created a new TreeNode and added sub nodes to it. Finally we added this node to the root node. When we execute the program, the following screen is produced.

217

Programmers Heaven: C# School

Tree View Events

Tree View has a number of important events, some of which are listed below.

Event

Description

AfterSelect

Fired when an item (node) is selected in the tree view control. TreeViewEventArgs are

 

passed with this event which contain:

 

1) TreeViewAction enumeration which describes the action caused the selection like

 

ByKeyboard, ByMouse, Collapse, etc

 

2) Node object which represents the selected node.

 

The selected node can also be accessed using the SelectedNode property of the tree view

 

control.

BeforeExpand

Fired just before the node is expanded.

BeforeCollapse

Fired just before the node is collapsed.

AfterExpand

Fired just after the node is expanded.

AfterCollapse

Fired just after the node is collapsed.

BeforeLabelEdit

Fired just before an attempt is made to edit the Label of the node. You need to set the

 

LabelEdit property of the tree view to true if you wish to allow your user change the label

 

of a node.

AfterLabelEdit

Fired just after the label of a node has been edited.

The following event handler will show the label of the node in a Message box whenever it is selected.

private void treeView1_AfterSelect(object sender, System.Windows.Forms.TreeViewEventArgs e)

{

MessageBox.Show("'" + e.Node.Text + "' node selected", "Selected Node");

}

218

Соседние файлы в папке books