Library code snippets

Clearing TreeView taking too much time

Recently I'd been working on an application that manipulates a large amount of hierarchical data in a TreeView. One of the application's menu commands (let's call it Clear) caused the TreeView to be cleared and repopulated again. Sometimes, the Clear command was executed in an instant. Sometimes, however, the Clear command took eternity.

After analyzing the problem, I've realized that the Clear command is slowest when the bottommost node in the TreeView is selected. That is, when one invokes the TreeView.Nodes.Clear method while a node in the TreeView is selected, the TreeView seems to be selecting all the nodes as they are being removed. Because node selection generates the AfterSelect event and because the event handler does quite a bit of work (lasting from 200 up to 800 milliseconds on a test machine), the Clear seemed to last forever.

Imagine for example a tree with 1000 nodes with the last node selected. Should the AfterSelect take only 100 milliseconds, the Clear would take almost 100 seconds!

Its odd, but the TreeView does work that way. If you don't believe me, have a look at this code that demonstrates the behavior.

If you do believe me, then please remember: the next time when you'll wonder why TreeView.Nodes.Clear call takes so much time, just replace the call with the following SmartClearNodes call:

Public Shared Sub SmartClearNodes(ByVal tree As TreeView)
  tree.BeginUpdate()
  Try
    tree.SelectedNode = Nothing ' this is it ;-)
    tree.Nodes.Clear()
  Finally
    tree.EndUpdate()
  End Try
End Sub

Comments

  1. 22 Jan 2009 at 21:24
    I tried this method using Visual Basic 2005. The time to clear a TreeView with over 20,000 nodes still takes the same amount of time (30 seconds in my tests). I have also tried disabling the Results TreeView and setting the visible property to false. Stll the same slow clearing speed. This also caused the program to take 30 seconds to leave memory when exiting the program. But that problem was overcome with the following code: Public Sub New() InitializeComponent() AddHandler backgroundWorker.RunWorkerCompleted, AddressOf backgroundWorker_Completed AddHandler backgroundWorker.ProgressChanged, AddressOf backgroundWorker_Progress AddHandler Me.FormClosing, AddressOf MikesClose End Sub Private Sub MyClose(ByVal sender As Object, ByVal e As FormClosingEventArgs) Process.GetCurrentProcess().Kill() End Sub Here is a piece of my code that calls the code described in the solution: Private Sub backgroundWorker_DoWork(ByVal sender As Object, ByVal e As DoWorkEventArgs) Handles backgroundWorker.DoWork backgroundWorker.ReportProgress(0) Try Me.Cursor = Cursors.WaitCursor ResultsGroupBox.Text = "Clearing Current Results" results.Update() results.Visible = False SmartClearNodes(results) results.Visible = True Catch ex As Exception Finally results.Enabled = True ResultsGroupBox.Text = "Searching Folders..." results.Update() End Try How can I make the time to clear the TreeView a second or two tops?
  2. 19 Nov 2005 at 06:35

    Hi


    Its a really help for me !
    Millions of thanks to u



    Manish Kaushik

  3. 13 Jan 2005 at 03:03
    Excellent Solution. I was breaking my head for this problem. Thanks
  4. 31 Dec 2004 at 07:29

    So simple, yet so effective!

  5. 01 Jan 1999 at 00:00

    This thread is for discussions of Clearing TreeView taking too much time .

Leave a comment

Sign in or Join us (it's free).

Palo Mraz I live in Slovakia with my wife, two sons (fulltime), one daughter (occasionally) and a dog. I've been doing Microsoft Windows development since 1988; primarily in VB. I'm a big fan of the MS .NET ...

Related podcasts

  • xpert to Expert: Inside Concurrent Basic (CB)

    "Concurrent Basic extends Visual Basic with stylish asynchronous concurrency constructs derived from the join calculus. Our design advances earlier MSRC work on Polyphonic C#, Comega and the Joins Library. Unlike its C# based predecessors, CB adopts a simple event-like syntax familiar to VB progr...

We'd love to hear what you think! Submit ideas or give us feedback