| Quickly clear the TreeView control with reverse deletion While VB's TreeView control offers the Clear method to remove the nodes from a TreeView, you've probably noticed that this command takes forever to execute. As a result, you may have searched for an alternative method. Believe it or not, one way to ramp up the TreeView's speed when clearing nodes is to delete them one by one in reverse order. To do so, you create a loop that steps backwards through the nodes, then issue the Remove method. The following code shows you how to implement this technique: Private Sub TrvwClear() Dim x As Integer With TreeView1.Nodes For x = .Count To 1 Step -1 .Remove x Next x End With End Sub With this code in place, the TreeView clears much faster than it does with the Clear method. Of course, if you want to use the API, you can improve this performance even further. We'll cover this improvement in the second part of this tip. ________________________________________________________________________ In the last tip, we showed you how to quickly clear the TreeView control of nodes by looping through the nodes in reverse order and then removing them. However, by using a simple API function, you can improve the performance of this technique. In the previous tip, we used the following code: Private Sub TrvwClear() Dim x As Integer With TreeView1.Nodes For x = .Count To 1 Step -1 .Remove x Next x End With End Sub Unfortunately, as VB loops through the TreeView's nodes and removes them, it also repaints the control to reflect the remaining nodes. This behavior slows down the process considerably. What's needed is a way to prevent the TreeView from repainting itself as it removes the nodes. Fortunately, the Windows API provides a great way to do just that. The SendMessage() function supports the WM_SETREDRAW message. With this message, your code can dictate whether a window should redraw (repaint) itself. Pass in True (or 1 in API parlance) to allow the window to repaint, or False (0) to prevent it. With the addition of the API to our code, the clear procedure now looks like this: Private Const WM_SETREDRAW = &HB Private Declare Function SendMessage Lib "user32" Alias _ "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As _ Long, ByVal wParam As Long, lParam As Any) As Long Private Sub TrvwClear() Dim x As Integer With TreeView1 SendMessage .hwnd, WM_SETREDRAW, 0, 0 For x = .Nodes.Count To 1 Step -1 .Nodes.Remove x Next x SendMessage .hwnd, WM_SETREDRAW, 1, 0 End With End Sub This combination of the API and reverse deletion provides the quickest way to clear the TreeView control. |
Quickly clear the TreeView control |
Freelance ASP PHP web development | Web developer India Web development India| Prayagasoft - web designer India, Ecommerce developer india, Ecommerce design