• 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.
  • Posts

    • Microsoft updates Media Creation Tool with a newer Windows 11 release by Taras Buria If you want to download a Windows 11 image or create an install media, the Media Creation Tool app is one of the best options—simple, fast, and, more importantly, official. While there are other useful alternatives, such as Rufus, Media Creation Tool remains the default choice for those who simply want a clean Windows 11 installation on a supported computer. Thanks to the latest MCT update, you will have to deal with fewer updates after installing Windows 11. Microsoft has quietly updated the Media Creation Tool app so that it downloads the latest Windows 11 release, version 24H2, with build number 26100.4349. This is the most recent public Windows 11 update (June 2025 Patch Tuesday), which eliminates the need to download large updates for your PC after installing the operating system. You can download Media Creation Tool from the official Windows 11 website. If you are familiar with it, this simple app can generate a Windows 11 image with the language you need or create a bootable USB media. Note that it only works on Windows, so Linux or macOS users have to download Windows images directly from the official website and then use apps that are available on their platforms. Another thing worth noting is Media Creation Tool is not the right choice if you want to install Windows 11 on an unsupported PC. In such a case, you can use MCT to download an ISO and then create a modified install media with apps like Rufus or Ventoy as described in our dedicated guide. If you want a more exotic Windows 11 Edition, try the Oofhours Media Tool, which allows you to select Windows 10 or 11, architecture (x64, x86, or ARM64), language, and edition. In addition to standard Home and Pro, the application can download non-mainstream SKUs, such as Enterprise, Education, or Professional for Workstations. Via: Deskmodder
    • Putting premium on your higher end laptops really makes it feel like your lower end laptops are worse.
    • Funny enough just removed insta account and whatsapp is next. Less power needed now
    • If you only show the stop code and what failed for less than 2 seconds, what use is it?
    • How about using all that hot air Zuck spews? That surely can power something...
  • Recent Achievements

    • Conversation Starter
      Kavin25 earned a badge
      Conversation Starter
    • One Month Later
      Leonard grant earned a badge
      One Month Later
    • Week One Done
      pcdoctorsnet earned a badge
      Week One Done
    • Rising Star
      Phillip0web went up a rank
      Rising Star
    • One Month Later
      Epaminombas earned a badge
      One Month Later
  • Popular Contributors

    1. 1
      +primortal
      538
    2. 2
      ATLien_0
      207
    3. 3
      +FloatingFatMan
      174
    4. 4
      Michael Scrip
      148
    5. 5
      snowy owl
      119
  • Tell a friend

    Love Neowin? Tell a friend!