Web Design that taps into the haromny and vision of your dreams.

List Boxes and Tree Views in C#

How to use Tree View and List Boxes in C#

Written By on in C#

846 words, estimated reading time 4 minutes.

Tree's, Lists and Grids are all very good ways of presenting data to the user, and they each have their own particular uses.

Windows Forms Applications Series
  1. Basics of C# Windows Forms Applications
  2. Menus, Popups and Toolbars in .Net Forms and C#
  3. Using Common Dialogue Boxes in C#
  4. List Boxes and Tree Views in C#
  5. List Box Data Binding in C# Windows Forms Application
  6. Using Multiple Forms in C# Applications with MDI
  7. Creating Controls for Windows Forms and C#
  8. Saving User Preferences and Settings within your C# Application

We have already seen list boxes briefly in a previous tutorial, but we will have a closer look at the list box and how to use it. All of these controls are data aware, that is they can be bound to a data source. We will have a look at Grids when we look at using data on Windows forms.

List Boxes and Combo Boxes

List boxes are used whenever you need to display a collection of items in a list format. If space is limited you can use a dropdown (combo) box instead. Both listBox and comboBox have the same methods for accessing the list items.

Adding and Removing Items

You can use the IDE interface to add items to a list, however, it is often required that we add items, or delete items, at runtime.

ListBox.Items.Add("Text to Show");

The add method will add a new item to the bottom of the list. You can use the Insert method to add an item at a specific location on the list, 0 will add the item as the first item.

ListBox.Items.Insert(5,"Text to Show");

Each item on a list box has an ItemIndex property to denote its position. The first item of the list has an ItemIndex of 0 (zero).

You can remove items from the list using the RemoveAt method, where you specify the index of the item to delete.

ListBox.Items.RemoveAt(5);

You can clear a list of all items using clear.

ListBox.Items.Clear();

Accessing Items and Selections

You can access an individual item through the Items collection to read and write items.

ListBox.Items[2] = "Hello World";
string myString = ListBox.Items[0];

Iterating through Items

There are two main methods for iterating through the items in the list, using a for and a for each loop.

for (int i = 0; i < listBox.Items.Count - 1; i++)
{
  MessageBox.Show(listBox.Items[i].ToString());
}

Because the listBox component accepts elements of any type, you must specify the type within the for each statement.

foreach (string O in listBox.Items)
{
  MessageBox.Show(O);
}

In both cases the Items collection is read-only, so cannot be assigned to.

Tree Views

Tree views are an organised list view the same as the Windows Explorer folder list view. It separates a list into sections that have subsections and so on. Like list and combo views, items can be added to a TreeView using the inbuilt IDE designer, and they can be modified at runtime. Unlike lists where an element is called an Item, each element of the tree is known as a Node.

To add a simple node to a Treeview

 TreeNode node = new TreeNode();
node.Name = "nodNewNode";
node.Text = "My New Node";
 
treeView1.Nodes.Add(node);
 

You can also add sub nodes to a node or to the tree view by creating a new TreeNode object for each item.

List Boxes and Tree Views in C#
List Boxes and Tree Views in C#
TreeNode node = new TreeNode();
node.Name = "nodNewNode";
node.Text = "My New Node";
 
TreeNode node2 = new TreeNode();
node2.Name = "nodNewNode2";
node2.Text = "My New Sub Node (a)";
node.Nodes.Add(node2);
 
TreeNode node3 = new TreeNode();
node3.Name = "nodNewNode2";
node3.Text = "My New Sub Node (b)";
node.Nodes.Add(node3);
 
treeView1.Nodes.Add(node);

If you try and reuse a node object after adding it to the tree view you will end up changing the items already assigned to the tree, as the TreeNode object is a reference type.

List Boxes and Tree Views in C#
List Boxes and Tree Views in C#
TreeNode node = new TreeNode();
node.Name = "nodNewNode";
node.Text = "My New Node";
 
TreeNode node2 = new TreeNode();
node2.Name = "nodNewNode2";
node2.Text = "My New Sub Node (a)";
node.Nodes.Add(node2);
 
node2.Name = "nodNewNode3";
node2.Text = "My New Sub Node (b)";
node.Nodes.Add(node2);

Nodes can be removed from the list using the Remove method and passing in the node you wish to remove.

treeView1.Nodes.Remove(node2);

If the above examples of node2 and node3 are not in scope you can Find a node by its name and remove it that way.

TreeNode[] nodes = treeView1.Nodes.Find("nodNewNode3", true);
foreach (TreeNode oldnode in nodes)
{
  treeView1.Nodes.Remove(oldnode);
}

This method will also delete all sub-nodes of the selected node, so if you passed in "nodNewNode" it would delete that node and the two sub-nodes and you will be left with an empty tree.

You can also remove nodes by specifying an index and using the RemoveAt method as described above.

Iterating through Items

From time to time it may be necessary to iterate through a tree view object. The only way to loop through tree view nodes is to use a recursive algorithm.

private void processNode(TreeNode node)
{
  MessageBox.Show(node.Text);
 
  foreach (TreeNode subnode in node.Nodes)
    processNode(subNode)
}
 
private void Test()
{
  foreach (TreeNode n in treeView1.Nodes)
  {
    processNode(n);
  }
}

Last updated on: Friday 23rd June 2017

 

Comments
Cormac Miller

Cormac Miller

>The only way to loop through treeview nodes is to use a recursive algorithm.
No it's not. If you can recurse, you can iterate, albeit not as elegantly...

Reply to Cormac Miller
Anish

Anish

Very useful article..Keep it up n thanks a lot..

Reply to Anish
Jason

Jason

You have done an awesome job with all of your tutorials dude, keep up the great work!!!!! I'd olve to see heaps more, I have learned so much from you.

Thank you,
Jason.

Reply to Jason

 

Leave a Reply

Your email address will not be published.





If you find something abusive or that does not comply with our terms or guidelines please flag it as inappropriate.

Copyright © 2001-2018 Tim Trott, all rights reserved. Web Design by Azulia Designs

This web page is licensed for your personal, private, non-commercial use only.

Disclaimer, Privacy & LegalSitemapContact Me