vb.net TreeView IComparer

时间:2013-09-13 13:41:49

标签: vb.net icomparer

为什么哦为什么这不起作用?我在MSDN上发现它但是引发了异常:Unable to cast object of type 'OutlookTemplates.com.NodeSorter' to type 'System.Collections.IComparer'.

Public Class NodeSorter
Implements IComparer(Of Windows.Forms.TreeNode)

Public Function Compare(ByVal x As Windows.Forms.TreeNode, ByVal y As Windows.Forms.TreeNode) _
    As Integer Implements IComparer(Of Windows.Forms.TreeNode).Compare
    Dim tx As Windows.Forms.TreeNode = CType(x, Windows.Forms.TreeNode)
    Dim ty As Windows.Forms.TreeNode = CType(y, Windows.Forms.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

....

 tv.TreeViewNodeSorter = New NodeSorter()
 tv.Sort()

1 个答案:

答案 0 :(得分:3)

您需要实现非通用 IComparer界面 (因为TreeView早于泛型)