I am using C#, .NET Framework 3.5, working with Windows Forms.
I have a TreeView and an Excel File. In this Excel File, there is a "kind of" tree. I want to represent this tree in the TreeView:
Key: Letter: Follow:
0 1 A 1 1
1 1 B 2 8
1 1 C 2 9
1 1 D 2 12
1 1 E 2 13
A
+-- B
+-- C
+-- D
+-- E
I can display this with the TreeViewer. But then there is one point, which doesn't work anymore:
Key: Letter: Follow:
0 1 A 1 1
1 1 B 2 8
1 1 C 2 9
1 1 D 2 12
1 1 E 2 13
2 8 W 3 10
2 8 X 3 10
2 9 Y 3 10
2 12 Z 3 10
3 10 WOOPS 4 1
A
+-- B
| +-- W
| +-- X
|
+-- C
| +-- Y
|
+-- D
| +-- Z
|
+-- E
WOOPS in this case has multiple parents. How can I add WOOPS in this tree?
Currently my solution is this:
TreeNode[] parents = treeView.Nodes.Find(l.Key, true);
for (int i = 0; i < parents.Length; i++)
{
parents[i].Nodes.Add(l.Follow, l.Letter);
treeView.Nodes.Find(l.Follow, true)[i].Tag = l;
}
Code Explanation:
l
(for letter) is an object with Key, Letter and Follow.- Letter A is already in there. If I now add B, the code above searches nodes with the key of B.
- I found A, and add my B with his Follow as the Nodekey and Letter as the name to A as a child.
- With the .Tag, I can save the object itselfs in there.
- This works with C, D and E, but also with W X Y Z.
- Now I want to add WOOPS.
- WOOPS finds not one parent, but 4.
- So I add him to all four parents.
Now here is the problem:
- WOOPS has, like A, four children.
- These four children are finding four WOOPS.
- Now I add these four children to four WOOPS.
- I added 4*4=16 childs.
This goes on and the parent.Length
gets bigger and bigger (it crashes my program).
How can I solve this (multiple parents) problem?
One possible solution I came up is, instead of adding parents and then children, I do the opposite way. First the childs, then the parents. I just copy the child and add it elsewhere (this copies also all of his childs). There won't be the "search and add" time problem.
The best solution for me would be, if I just add WOOPS once, and the TreeView only links to these objects. So if I open up W, it shows WOOPS. And if I open up X, it shows WOOPS too as his child. But it is the same object. Is this even possible?
EDIT:
The Letter is a string (not char). The Key is a string too. The Follow is a string too.
Those "numbers" are their key and the follow ups. If the key has a follow up (in this case BCDE has their follow in A), so they are under A. I hope this helps.