Home
Map
TreeView TutorialUse the TreeView control in Windows Forms to display nested data items and allow expanding of child nodes.
WinForms
This page was last reviewed on Sep 29, 2022.
TreeView. This displays text and icon data. TreeView must have nodes added to it through the Nodes collection. It represents hierarchical text and icon data in a convenient way.
Get started. We add a TreeView control to the Windows Forms Application project. To do this, open the Toolbox panel by clicking on the View and then Toolbox menu item in Visual Studio.
Add example. Double-click on the Form1 window to create the Form1_Load event. In this event handler, we will insert code to build the nodes in the TreeView control.
Info To add the Form1_Load event, double-click on the window of the program in the Visual Studio designer.
Note The Nodes.Add() call adds a node instance to the treeView1 instance of the TreeView that exists.
Detail You can call the Add instance method and pass it an argument of type TreeNode to add a node to the TreeView.
using System; using System.Windows.Forms; namespace WindowsFormsApplication23 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { // // This is the first node in the view. // TreeNode treeNode = new TreeNode("Windows"); treeView1.Nodes.Add(treeNode); // // Another node following the first node. // treeNode = new TreeNode("Linux"); treeView1.Nodes.Add(treeNode); // // Create two child nodes and put them in an array. // ... Add the third node, and specify these as its children. // TreeNode node2 = new TreeNode("C#"); TreeNode node3 = new TreeNode("VB.NET"); TreeNode[] array = new TreeNode[] { node2, node3 }; // // Final node. // treeNode = new TreeNode("Dot Net Perls", array); treeView1.Nodes.Add(treeNode); } } }
Click. We can add a double-click event handler to your TreeView program. This means that when the user double-clicks on an item, you can execute code based on that node.
Tip To add the MouseDoubleClick event handler, right-click the TreeView in the designer and select Properties.
Then Select "MouseDoubleClick" and click twice on that entry. You can then change treeView1_MouseDoubleClick, which was generated.
using System; using System.Windows.Forms; namespace WindowsFormsApplication23 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { // Insert code here. [OMIT] } private void treeView1_MouseDoubleClick(object sender, MouseEventArgs e) { // // Get the selected node. // TreeNode node = treeView1.SelectedNode; // // Render message box. // MessageBox.Show(string.Format("You selected: {0}", node.Text)); } } }
Using SelectedNode property. In the treeView1_MouseDoubleClick method, you can see that the SelectedNode property is accessed on the treeView1 control.
And This returns the selected node if one is selected. After a double-click, a node is selected so this returns a reference to that node.
Using Text property. The treeView1_MouseDoubleClick event handler also accesses the Text instance property on the treeView1 control.
And This returns a reference to the string that represents the text in the TreeNode.
Tip If you double-click on the node "Linux", a MessageBox is shown with that text.
MessageBox.Show
Summary. We added TreeNode references and children nodes to a TreeView. Next, we added an event handler, which enables primitive interaction with the TreeView nodes by the user.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Sep 29, 2022 (edit).
Home
Changes
© 2007-2024 Sam Allen.