다음을 통해 공유


TreeView.TreeViewNodeSorter 속성

정의

IComparer 노드의 사용자 지정 정렬을 수행하는 TreeView 구현을 가져오거나 설정합니다.

public:
 property System::Collections::IComparer ^ TreeViewNodeSorter { System::Collections::IComparer ^ get(); void set(System::Collections::IComparer ^ value); };
[System.ComponentModel.Browsable(false)]
public System.Collections.IComparer TreeViewNodeSorter { get; set; }
[System.ComponentModel.Browsable(false)]
public System.Collections.IComparer? TreeViewNodeSorter { get; set; }
[<System.ComponentModel.Browsable(false)>]
member this.TreeViewNodeSorter : System.Collections.IComparer with get, set
Public Property TreeViewNodeSorter As IComparer

속성 값

사용자 지정 정렬을 수행할 IComparer입니다.

특성

예제

다음 코드 예제를 사용 TreeViewNodeSorter 하는 방법에 설명 합니다 가장 작은에서 가장 큰 노드를 정렬 하는 속성입니다. 이 예제를 실행하려면 다음 코드를 Windows Form에 붙여넣고 폼의 생성자 또는 Load 이벤트 처리기에서 를 호출 InitializeTreeView1 합니다.

    // 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();
    }
// Declare the TreeView.
private TreeView treeView1;
private TextBox textBox1;
private Button button1;

private void InitializeTreeView1()
{
    // Create the TreeView
    treeView1 = new TreeView();
    treeView1.Size = new Size(200, 200);

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

    // Handle the click event for the button.
    button1.Click += new EventHandler(button1_Click);

    // Create the root nodes.
    TreeNode docNode = new TreeNode("Documents");
    TreeNode spreadSheetNode = new 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.
    treeView1.Nodes.Add(spreadSheetNode);
    treeView1.Nodes.Add(docNode);

    // Add the TreeView to the form.
    Controls.Add(treeView1);
    Controls.Add(button1);
}

// Set the TreeViewNodeSorter property to a new instance
// of the custom sorter.
private void button1_Click(object sender, EventArgs e)
{
    treeView1.TreeViewNodeSorter = new NodeSorter();
}

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

        // 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 WithEvents treeView1 As TreeView
Private textBox1 As TextBox
Private WithEvents button1 As Button


Private Sub InitializeTreeView1()

    ' Create the TreeView
    treeView1 = New TreeView()
    treeView1.Size = New Size(200, 200)

    ' Create the button and set some basic properties. 
    button1 = New Button()
    button1.Location = New Point(205, 138)
    button1.Text = "Set Sorter"

    ' Create the root nodes.
    Dim docNode As New TreeNode("Documents")
    Dim spreadSheetNode As New 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.
    treeView1.Nodes.Add(spreadSheetNode)
    treeView1.Nodes.Add(docNode)

    ' Add the TreeView to the form.
    Controls.Add(treeView1)
    Controls.Add(button1)

End Sub


' Set the TreeViewNodeSorter property to a new instance
' of the custom sorter.
Private Sub button1_Click(ByVal sender As Object, _
    ByVal e As EventArgs) Handles button1.Click

    treeView1.TreeViewNodeSorter = New NodeSorter()

End Sub

' Create a node sorter that implements the IComparer interface.

Public Class NodeSorter
    Implements IComparer
    
    ' Compare the length of the strings, or the strings
    ' themselves, if they are the same length.
    Public Function Compare(ByVal x As Object, ByVal y As Object) _
        As Integer Implements IComparer.Compare
        Dim tx As TreeNode = CType(x, TreeNode)
        Dim ty As TreeNode = CType(y, TreeNode)
       
        If tx.Text.Length <> ty.Text.Length Then
            Return tx.Text.Length - ty.Text.Length
        End If
        Return String.Compare(tx.Text, ty.Text)

    End Function
End Class

설명

사용자 지정 정렬은 가 TreeViewNodeSorter 설정되면 적용됩니다.

레이블이 변경된 경우 메서드를 TreeNodeSort 호출하여 속성에 지정된 사용자 지정 정렬기를 사용하여 항목을 다시 정렬할 TreeViewNodeSorter 수 있습니다.

참고

기본 정렬 합니다 Compare 기반 메서드는 CurrentCulture 애플리케이션에 지정 합니다. 즉, 값이 TreeNode 같은 개체는 컨트롤에 추가된 순서대로 TreeView 유지됩니다. 사용자 지정 정렬이 적용되는 경우 이 동작은 다를 수 있습니다.

적용 대상

추가 정보