Populating a treeview from Text

M

Maddy

I need to populate a treeview from a text file. The data in it is in
the following form

1. (Parent Node)
(a) (Child Node)
(b) (Child Node)
..
..
..
(k) (Child Node)

2. (Parent Node)
(a) (Child Node)
and so on..

The data in the treeview will remain static. I do not need any dynamic
loading or update. It would be best if I can load the data into the
treeview during design time. I want an automated process as there are
over 50 parent nodes each with 10-15 child nodes. I dont want to sit
and type each one individually.

Thanks
Nitin
 
M

Morten Wennevik [C# MVP]

Maddy said:
I need to populate a treeview from a text file. The data in it is in
the following form

1. (Parent Node)
(a) (Child Node)
(b) (Child Node)
..
..
..
(k) (Child Node)

2. (Parent Node)
(a) (Child Node)
and so on..

The data in the treeview will remain static. I do not need any dynamic
loading or update. It would be best if I can load the data into the
treeview during design time. I want an automated process as there are
over 50 parent nodes each with 10-15 child nodes. I dont want to sit
and type each one individually.

Thanks
Nitin

Hi Nitin,

If you read the file, parse and create TreeNodes during a UserControl load
event, the data will be visible during design time if you put the UserControl
in another Form or UserControl. Just create a UserControl and add a TreeView
with DockStyle.Filled.
 
M

Maddy

If you read the file, parse and create TreeNodes during a UserControl load
event, the data will be visible during design time if you put the UserControl
in another Form or UserControl.  Just create a UserControl and add a TreeView
with DockStyle.Filled.

Thanks for the response. But I am a newbie. Could you please tell in a
little more detail. For beginning, I dont know what a UserControl
means. (Sorry, just started on c#). Would really appreciate step wise
explanation or if you can direct me where I can get the details as I
could not find anything either on the net or the vs2008 help.
Thanks
Nitin
 
M

Morten Wennevik [C# MVP]

Maddy said:
Thanks for the response. But I am a newbie. Could you please tell in a
little more detail. For beginning, I dont know what a UserControl
means. (Sorry, just started on c#). Would really appreciate step wise
explanation or if you can direct me where I can get the details as I
could not find anything either on the net or the vs2008 help.
Thanks
Nitin

You create a UserControl by right clicking on your project and select
Add->UserControl. Call this something like MyTreeView. You should end up
with the UserControl in design mode.

Drag a TreeView into the UserControl and set the Dock property to Fill.

Open the code view for the UserControl (right click the control and select
code view).

Override OnLoad and add the TreeNode-loading code inside. The code sample
below adds a selection of nodes from a string array. You can get the string
array from a file using File.ReadAllLines().

When you compile this you should be able to see "MyTreeView" as a control in
the ToolBox. If you drag this onto any control, the nodes should be filled
in the designer. You won't be able to click them though, and if the node
loading code is flawed, you need to add a control manually and run the
program to be able to debug it.

public partial class MyTreeView : UserControl
{
public MyTreeView()
{
InitializeComponent();
}

protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);

string[] nodes = new string[] {
"0|1|Hello",
"0|2|Goodbye",
"1|3|World",
"2|4|World" };

foreach (string s in nodes)
{
string[] data = s.Split('|');
string parentId = data[0];
string id = data[1];
string value = data[2];

if (parentId == "0")
treeView1.Nodes.Add(value).Tag = id;
else
{
TreeNode node = FindNode(treeView1.Nodes, parentId);
if (node != null)
node.Nodes.Add(value).Tag = id;
else
throw new InvalidOperationException("Incorrect data
order");
}
}

treeView1.ExpandAll();
}

private TreeNode FindNode(TreeNodeCollection treeNodeCollection, string
parentId)
{
foreach (TreeNode node in treeNodeCollection)
{
if (node.Tag.ToString() == parentId)
return node;
else
{
TreeNode parent = FindNode(node.Nodes, parentId);
if (parent != null)
return parent;
}
}

return null;
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top