• 0

Extremely weird C# problem


Question

I have a really weird problem with TreeView control. Has to do with how it's populated and subsequently cleared.

Context:

I have a collection of data objects (List). Each object in the collection has property that returns its associated TreeNode object. I selectively populate List<TreeNode> with these nodes and then add those nodes to TreeView:

List&lt;TreeNode&gt; nodes = new List&lt;TreeNode&gt;(capacity);

foreach (MyObject obj in myList)
{
		if (obj.SomeProperty == true) // condition
				 nodes.Add(obj.TreeNode);
}

TreeNode[] node_array = nodes.ToArray();

TreeView.Nodes.AddRange(node_array);

At some point I clear TreeView using TreeView.Nodes.Clear();

Problem:

Whenever ALL nodes are added (condition yields true for every obj in the list), TreeView.Nodes.Clear() is very fast. However, if enough nodes are not added (when condition is false for some objects), TreeView.Nodes.Clear() is extremely slow.

This makes absolutely no sense. TreeView is cleared fast whenever all available nodes are present, yet it is very very slow whenever some nodes are missing. All nodes are completely independent from each other.

Can anyone provide an explanation as to why this happens? I've been banging my head against the wall for the last 3 hours.

Thanks in advance.

Link to comment
https://www.neowin.net/forum/topic/656736-extremely-weird-c-problem/
Share on other sites

4 answers to this question

Recommended Posts

  • 0

It may be that the treeview is redrawing upon each removal. If the treeview has greater than 200 nodes, it doesn't redraw until all the nodes are removed. If 200 or less nodes, it will redraw upon each removal. Other than that, I cannot see another reason why it would be behaving as it is for you.

Special thanks to Lutz Roeder's .NET Reflector. ;)

  • 0
  azcodemonkey said:
It may be that the treeview is redrawing upon each removal. If the treeview has greater than 200 nodes, it doesn't redraw until all the nodes are removed. If 200 or less nodes, it will redraw upon each removal. Other than that, I cannot see another reason why it would be behaving as it is for you.

Special thanks to Lutz Roeder's .NET Reflector. ;)

Thanx!!!

tree.BeginUpdate();

tree.Nodes.Clear();

tree.EndUpdate();

This solved the problem.

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.