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<TreeNode> nodes = new List<TreeNode>(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.
Question
remix17
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:
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