[C# Helper]
Index Books FAQ Contact About Rod
[Beginning Database Design Solutions, Second Edition]

[Beginning Software Engineering, Second Edition]

[Essential Algorithms, Second Edition]

[The Modern C# Challenge]

[WPF 3d, Three-Dimensional Graphics with WPF and C#]

[The C# Helper Top 100]

[Interview Puzzles Dissected]

[C# 24-Hour Trainer]

[C# 5.0 Programmer's Reference]

[MCSD Certification Toolkit (Exam 70-483): Programming in C#]

Title: Load a TreeView from a tab-delimited file in C#

example

This example shows how to Load a TreeView from a tab-delimited file. When it starts, the program loads the following text file into its TreeView control.

Food Vegetables Beans Peas Lettuce Desserts Ice Cream Cookies Peanut Butter Chip Chocolate Chip Cake Fruit Bananas Peaches Sports Volleyball Baseball

The following code shows how the program loads its TreeView control.

// Load a TreeView control from a file that uses tabs // to show indentation. private void LoadTreeViewFromFile(string file_name, TreeView trv) { // Get the file's contents. string file_contents = File.ReadAllText(file_name); // Break the file into lines. string[] lines = file_contents.Split( new char[] {'\r', '\n'}, StringSplitOptions.RemoveEmptyEntries); // Process the lines. trv.Nodes.Clear(); Dictionary parents = new Dictionary(); foreach (string text_line in lines) { // See how many tabs are at the start of the line. int level = text_line.Length - text_line.TrimStart('\t').Length; // Add the new node. if (level == 0) parents[level] = trv.Nodes.Add(text_line.Trim()); else parents[level] = parents[level - 1].Nodes.Add(text_line.Trim()); parents[level].EnsureVisible(); } if (trv.Nodes.Count > 0) trv.Nodes[0].EnsureVisible(); }

First the program uses System.IO.File.ReadAllText to read the text file into a string. It then splits the file into lines using \r and \n characters as delimiters. It removes any blank lines, which may represent actual blank lines or may be due to the file containing \r\n combinations. (This is why the program doesn't just use ReadAllLines. The Split method lets is easily remove blank lines but ReadAllLines doesn't.)

Next the code clears the TreeView control and creates the parents dictionary to hold the most recent node at each level of the tree. For example, parents[2] will hold the most recently added node at level 2 in the tree. Later, if the program needs to add a node at level 3 of the tree, it can use parents[2] as its parent. Initially parents is empty.

The then code loops over the lines in the file. For each line, it counts the number of tab characters are at the beginning of the line to determine the node's level in the tree. It does that by using Trim to remove leading tab characters and then subtracting the string's new length from its original length.

Now the code creates a new node for the line. If the node is at level 0, the program adds it to the TreeView's Nodes collection to make it a top-level node. If the node is not at level 0, the code adds it as a child of the previously saved node at level - 1 in the tree, which was recorded in parents[level - 1]. In either case, the code saves the new node in parents[level] in case the program needs to add a node as a child of this one.

After adding the node, the program calls the node's EnsureVisible method to make sure its parent is expanded.

After adding all of the nodes, the code ensures the first node is visible so the TreeView displays its top node if it is too long to show all of the nodes at the same time.

Download the example to experiment with it and to see additional details.

© 2009-2023 Rocky Mountain Computer Consulting, Inc. All rights reserved.