Export (0) Print
Expand All
1 out of 1 rated this helpful - Rate this topic

TreeView::TreeViewNodeSorter Property

Gets or sets the implementation of IComparer to perform a custom sort of the TreeView nodes.

Namespace:  System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)
[BrowsableAttribute(false)]
public:
property IComparer^ TreeViewNodeSorter {
	IComparer^ get ();
	void set (IComparer^ value);
}

Property Value

Type: System.Collections::IComparer
The IComparer to perform the custom sort.

The custom sort is applied when the TreeViewNodeSorter is set.

If a TreeNode label is changed, you can call the Sort method to sort the items again with the custom sorter specified by the TreeViewNodeSorter property.

NoteNote

The default sorter uses the Compare method based on the CurrentCulture specified by the application. This means that TreeNode objects with equal value are kept in the order in which they were added to the TreeView control. This behavior may be different if a custom sort is applied.

The following code example demonstrates how to use the TreeViewNodeSorter property to sort nodes from smallest to largest. To run this example, paste the following code into a Windows Form and call InitializeTreeView1 from the form's constructor or Load event handler.

    // Create a node sorter that implements the IComparer interface. 
private:
    ref class NodeSorter : public IComparer
    {
        // Compare the length of the strings, or the strings 
        // themselves, if they are the same length. 
    public:
        virtual int Compare(Object^ x, Object^ y)
        {
            TreeNode^ tx = (TreeNode^)x;
            TreeNode^ ty = (TreeNode^)y;

            // Compare the length of the strings, returning the difference. 
            if (tx->Text->Length != ty->Text->Length)
            {
                return (tx->Text->Length - ty->Text->Length);
            }

            // If they are the same length, call Compare. 
            return String::Compare(tx->Text, ty->Text);
        }
    };

    // Declare the TreeView. 
private:
    TreeView^ initialTreeView;
private:
    TextBox^ displayNodes;
private:
    Button^ setSorter;

private:
    void InitializeInitialTreeView()
    {
        // Create the TreeView
        initialTreeView = gcnew TreeView();
        initialTreeView->Size = System::Drawing::Size(200, 200);

        // Create the button and set some basic properties.
        setSorter = gcnew Button();
        setSorter->Location = Point(205, 138);
        setSorter->Text = "Set Sorter";

        // Handle the click event for the button.
        setSorter->Click += gcnew EventHandler(this, 
            &TreeViewWhidbeyMembersExample::SetSorter_Click);

        // Create the root nodes.
        TreeNode^ docNode = gcnew TreeNode("Documents");
        TreeNode^ spreadSheetNode = gcnew TreeNode("Spreadsheets");

        // Add some additional nodes.
        spreadSheetNode->Nodes->Add("payroll.xls");
        spreadSheetNode->Nodes->Add("checking.xls");
        spreadSheetNode->Nodes->Add("tracking.xls");
        docNode->Nodes->Add("phoneList.doc");
        docNode->Nodes->Add("resume.doc");

        // Add the root nodes to the TreeView.
        initialTreeView->Nodes->Add(spreadSheetNode);
        initialTreeView->Nodes->Add(docNode);

        // Add the TreeView to the form. 
        this->Controls->Add(initialTreeView);
        this->Controls->Add(setSorter);
    }

    // Set the TreeViewNodeSorter property to a new instance 
    // of the custom sorter. 
private:
    void SetSorter_Click(Object^ sender, EventArgs^ e)
    {
        initialTreeView->TreeViewNodeSorter = gcnew NodeSorter();
    }

.NET Framework

Supported in: 4.5.1, 4.5, 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.