Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sort TreeView Automatically Upon Adding Nodes

Is there an easy way to add nodes to a WinForms .NET TreeView control where the new nodes being added are inserted at the correct index so the entire list of nodes is sorted alphabetically? Pretty much having the same result as TreeView.Sort().

I have a TreeView that continually grows to a couple hundred nodes. The user can view this TreeView in real time as it grows. I'd prefer to just insert the nodes at the correct index, rather than calling TreeView.Sort() each time after a node is added.

Can this be done?

like image 530
Matt Hanson Avatar asked Dec 24 '08 05:12

Matt Hanson


People also ask

How do I know if TreeView node is selected?

Solution 1Use TreeView. SelectedNode[^] property, which get or set selected node for currently selected Treeview. If no TreeNode is currently selected, the SelectedNode property is a null reference (Nothing in Visual Basic).

How does TreeView work?

The TreeView control is used to display hierarchical representations of items similar to the ways the files and folders are displayed in the left pane of the Windows Explorer. Each node may contain one or more child nodes. Let's click on a TreeView control from the Toolbox and place it on the form.

What is use of TreeView control?

A tree-view control is a window that displays a hierarchical list of items, such as the headings in a document, the entries in an index, or the files and directories on a disk. Each item consists of a label and an optional bitmapped image, and each item can have a list of subitems associated with it.


2 Answers

In winforms, you can simply set the TreeView's .Sorted property to True.

When Sorted is set to true, the TreeNode objects are sorted in alphabetical order by their Text property values. You should always use BeginUpdate and EndUpdate to maintain performance when adding a large quantity of items to a sorted TreeView. When the text of an existing node is changed, you must call Sort to resort the items.

Ref MSDN

like image 176
Mitch Wheat Avatar answered Sep 18 '22 07:09

Mitch Wheat


Why don't you create new classes that inherits from TreeView and TreeNodeCollection? The new TreeView will use your new TreeNodeCollection and you can override the Add() method of the TreeNodeCollection to do what you're suggesting.

The method would have to: 1. Find the correct position to insert into and 2. Insert the new node.

The most trivial implementation would iterate through the collection until thisNode.value<=newNode.value<nextNode.value. Then insert before nextNode.Index. You could see a performance increase if you use a different search algorithm, depending on the size of the collection. (Something like a binary search comes to mind.)

Note: You could also just create an extension method on a TreeNodeCollection that does the same thing. However, overriding the Add() method ensures your TreeView is always sorted. Creating only an extension method may lead to undefined results if it is not already sorted before your AddIntoSorted() call.

like image 23
lc. Avatar answered Sep 21 '22 07:09

lc.