|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace TreeViewExample
{
public partial class Form1 : Form
{
public class kData { // instead of class you can use struct, but with some dificulties
public int index;
public int level;
public string NodeText;
public string OtherData;
//public stData() { } // here you can initialize variables
};
List<kData> L; // instead of list you can use array, but I prefer Lists
public Form1() {
InitializeComponent();
}
private void btnPopulate_Click(object sender, EventArgs e) {
// take data from ListBox and store it to List
string line;
L = new List<kData>();
for (int i = 0; i < lbData.Items.Count; i++) {
line = lbData.Items[i].ToString();
L.Add(new kData());
L[i].index = i;
L[i].level = Convert.ToInt32(line.Substring(0,1));
L[i].NodeText = line.Substring(2);
L[i].OtherData = "some other data you might need";
}
populateBaseNodes();
}
public void populateBaseNodes()
{
int i;
tvData.Nodes.Clear();
tvData.BeginUpdate();
for (i = 0; i < L.Count(); i++) {
if (L[i].level == 0) {
tvData.Nodes.Add(L[i].NodeText, L[i].NodeText);
tvData.Nodes[tvData.Nodes.Count - 1].Tag = L[i];
}
}
for (i = 0; i < tvData.Nodes.Count; i++)
populateChilds(tvData.Nodes[i]);
tvData.EndUpdate();
tvData.Refresh();
}
public void populateChilds(TreeNode parentNode)
{
kData parentRed = (kData)parentNode.Tag;
for (int i = parentRed.index + 1; i < L.Count; i++) {
if (L[i].level == (parentRed.level + 1)) {
parentNode.Nodes.Add(L[i].NodeText, L[i].NodeText);
parentNode.Nodes[parentNode.Nodes.Count - 1].Tag = L[i];
populateChilds(parentNode.Nodes[parentNode.Nodes.Count - 1]);
}
if (L[i].level <= parentRed.level) break;
}
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.