문제

Why oh why doesn't this work? I found it on MSDN butit throws an exception: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()
도움이 되었습니까?

해결책

You need to implement the non-generic IComparer interface.
(because TreeView predates generics)

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top