0

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:

  1. l (for letter) is an object with Key, Letter and Follow.
  2. Letter A is already in there. If I now add B, the code above searches nodes with the key of B.
  3. I found A, and add my B with his Follow as the Nodekey and Letter as the name to A as a child.
  4. With the .Tag, I can save the object itselfs in there.
  5. This works with C, D and E, but also with W X Y Z.
  6. Now I want to add WOOPS.
  7. WOOPS finds not one parent, but 4.
  8. So I add him to all four parents.

Now here is the problem:

  1. WOOPS has, like A, four children.
  2. These four children are finding four WOOPS.
  3. Now I add these four children to four WOOPS.
  4. 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.

EscanorEU
  • 96
  • 7
  • I appreciate how you tried to explain your problem, but it's not clear enough about what you are doing. Why add a string suddenly? since it's all just chars? and why does this string have multiple parents? i don't see the connection. and those numbers don't make much sense to me. :-\ – Zorkind Jan 05 '18 at 11:19
  • perhaps add what's the requirements/rules of this tree. i know you haven't said anywhere its just chars, but without the info, i end up guessing. – Zorkind Jan 05 '18 at 11:23
  • Thank you for your answer. It is a simplified version. I'll edit it. Those letters are normally strings. It doesn't matter if these are chars or strings. This "WOOPS" should symbolize my problem. It has multiple parents, because the "parents" all link to this one child. Everything else after this point, the childs are the same. – EscanorEU Jan 05 '18 at 11:31
  • Understood, i would be careful with the tag tho, perhaps its better to have an array in memory with those objects and save the array index in the tag, so you don't have duplicates and other weird issues. – Zorkind Jan 05 '18 at 11:36
  • Thank you for the tip. I will remember that. – EscanorEU Jan 05 '18 at 11:38
  • I think a good way to solve your problem is making a lazy load of children when you expand the tree. This way you don't run out of memory holding all these replications, it will get huge if you load all the parents of all children and all children of all parents, on and on forever. – Zorkind Jan 05 '18 at 11:41
  • You can use the ´AfterExpand´ event to do it. Take a look at this as an example of that https://stackoverflow.com/questions/1851729/treeview-child-node-populating-problem – Zorkind Jan 05 '18 at 11:44
  • Thanks, I will look into it. – EscanorEU Jan 05 '18 at 11:47
  • @Zorkind Looks good, but how can I view my custom form? – EscanorEU Jan 05 '18 at 12:51
  • I am not sure what you mean. – Zorkind Jan 05 '18 at 12:51
  • @Zorkind Thanks, I found out how. In Visual Studio, it's simply in the Toolbox :) – EscanorEU Jan 05 '18 at 12:54

2 Answers2

0

I think i understand what you want to achieve. I suggest to change

 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;
 }

to this:

 TreeNode[] parents = treeView.Nodes.Find(l.Key, true);

 TreeNode node = new TreeNode(l.Letter);  
 node.Tag = l;

 for (int i = 0; i < parents.Length; i++)
 {
     parents[i].Nodes.Add(node);
 }

So basically, you create the node outside your loop and reference to the node multiple times.

EDIT: code corrected according to comment

Craylen
  • 313
  • 2
  • 12
  • Hi, `TreeNode node = new TreeNode(l.Follow, l.Letter);` doesn't work. But I created a solution where this works. The problem is, it takes the same amount of time. Thank you for helping! – EscanorEU Jan 05 '18 at 12:29
  • i've corrected the code example, just for completition :) – Craylen Jan 05 '18 at 13:25
  • Without an own TreeNode class, you cannot find the `node` anymore. It doesn't have a Nodekey. – EscanorEU Jan 05 '18 at 14:20
0

I found the solution. @Zorkind comment helped me a lot. Basically it searches only, after I expand the node. So I won't have the scaling problem. Also, I created the tree recursive, which saves also a lot of time (kind-of: Are there childs? Then add them to the tree, call the function again, and so on).

It looks basically real-time, but it isn't.

EscanorEU
  • 96
  • 7