• 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

    • I enjoy using Discover Weekly. I have found new music using it. Just sometimes it goes off the rails and doesn't remember I don't like rap. I'm not sure this update is anything helpful, but I guess I'll try it if it comes to free accounts.
    • Yep, I got 250 mbps down / 50 mbps up on Qatar last week and about a 30 ms ping.
    • Here are all the new features added to Microsoft 365 Copilot in June 2025 by Usama Jawad Towards the end of each month, Microsoft publishes a roundup of the features that it added to some of its popular software in the previous four weeks. We have already talked about the new capabilities introduced in Excel and Teams during the month of June 2025, and now, it's time to talk about Microsoft 365 Copilot. We'll start off with admin-facing capabilities since there are only a few of them. For starters, the usage metrics for Copilot in the Copilot Analytics tool now have new prompt categories that give more insights as to how users are engaging with Copilot. This feature has just begun rolling out, but another enhancement to the usage metrics that is already available is dedicated statistics for intelligent meeting recaps. Finally, Microsoft 365 admins can now view and manage their inventory of agents and connectors and also have more granular control over costs and billing policies. On the user side of things, we have intelligent assistance in Copilot Chat, powered by ContextIQ. This layer of intelligence can scope prompts to internal (SharePoint, OneDrive) and external data sources, find files in the chat, and proactively offer relevant suggestions as you type. In the same vein, the Copilot mobile app is being updated so you can talk to the AI in a natural manner using your voice. In addition, users can also get access to deep reasoning agents such as Researcher and Analyst for more complex and research-oriented needs. The Create experience in the app is also being updated with the ability to generate stories and branded templates. Other interesting Copilot capabilities rolling out to Edge customers are the ability to prompt the AI through the search bar, access agents from within the browser, and take advantage of Copilot's impressive text summarization capabilities. That's not all though, other features in tow include: Enhancements to Copilot in Outlook: Schedule meetings through Copilot chat, summarization of email attachments, a new sidebar experience in the classic Outlook app, meeting preparation, and automated meeting invite creation Improved image generation and large file handling in Copilot Chat: More photorealistic image generation with better text depiction, ability to generate longer summaries from bigger files, and PDF scanning capabilities for insights Memory in Copilot: Copilot will now remember certain items from your conversation and you can modify or delete them Transferred calls summary with Copilot in Teams Phone: Generate a summary of a call and transfer it to a target New file extension for Copilot Pages: Copilot Pages will now have .page extension with an updated file icon Copilot Notebooks availability in OneNote: We already covered this in detail here Seamlessly add brand-approved images with Copilot in PowerPoint: Integration of Copilot with SharePoint Organization Asset Library (OAL) and Templafy asset libraries Explain formulas on the grid with Copilot in Excel: Self-explanatory, exactly what it says on the tin Expanded availability for the Microsoft 365 Copilot app: Availability of the Microsoft 365 Copilot app on Mac You can read more details about each of the aforementioned features here.
    • Damn, I blocked OldGuru a long time ago and you have to go and quote them so I have to read that creepy a$$ take. LOL Anyway 100% that dude can't find women that will have sex with him.
    • OneNote for Windows gets support for Dynamic DPI by Usama Jawad OneNote for Windows (part of Microsoft 365) is a pretty useful app if you're actively engaged in note-taking activities and also appreciate some rich text formatting capabilities. In fact, it also offers some decent integrations with Copilot, which make it an important piece of software in productivity-based environments. Now, Microsoft has introduced a feature that will likely make people with multi-monitor setups very happy. The OneNote for Windows application now supports Dynamic DPI (dots per inch). What this means is that you can use OneNote across any screen and it will scale according to the display's resolution, and you won't get a disconcerting and distracting blurring effect. You can extend your display to a high-resolution monitor and shift OneNote across displays without a hitch or any distraction. This is similar to the UX that is already present in Word, Excel, and PowerPoint. This Dynamic DPI support not only extends to the main text area but also to the section tabs, the Notebooks pane, drop-down menus, and Copilot Notebooks. All of these should look crisp and polished moving forward, without any manual adjustment or even an app restart required from the user's side. Microsoft has highlighted that it was encouraged to work on this capability after receiving user feedback from customers in this area. Dynamic DPI is now available to Current Channel (CC) customers on OneNote for Windows, running Version 2504 (Build 16.0.18827.20042) or later. That's not all, though. Another smaller enhancement present in OneNote moving forward is a revamped setup experience when you launch OneNote on a new Windows device for the first time. You will now receive a list of your five most recently used (MRU) notebooks that will open instantaneusly with a click. If you have more than five notebooks, you can pick and choose the files that you want to open. That said, Microsoft is looking to expand and improve on this experience in the future since it is fairly limited right now.
  • Recent Achievements

    • Week One Done
      dennis Nebeker earned a badge
      Week One Done
    • One Year In
      timothytoots earned a badge
      One Year In
    • One Month Later
      CHUNWEI earned a badge
      One Month Later
    • Week One Done
      TIGOSS earned a badge
      Week One Done
    • First Post
      henryj earned a badge
      First Post
  • Popular Contributors

    1. 1
      +primortal
      464
    2. 2
      +FloatingFatMan
      194
    3. 3
      ATLien_0
      163
    4. 4
      Xenon
      78
    5. 5
      Som
      73
  • Tell a friend

    Love Neowin? Tell a friend!