我想我找到了一種方法來解決它(只是一個測試程序,所以我用函數返回的測試信息,而不是方法和短變量名):
Public Class TestListClass(Of T)
Dim Current As New List(Of T)
Dim Prev As New List(Of T)
Public Function add(T1 As List(Of T)) As String
Dim s As String = String.Empty
If T1.GetType.IsGenericType Then ' better safe then sorry and we'll need that in the final version
Dim ar As Type() = T1.GetType.GenericTypeArguments
s = ar(0).ToString
End If
If Current.Count = 0 Then
For Each X As T In T1
Current.Add(X)
Next
Return "created " & s & vbNewLine
Else
If Prev.Count > 0 Then Prev.Clear()
For Each X As T In Current
Prev.Add(X)
Next
Current.Clear()
For Each X As T In T1
Current.Add(X)
Next
Return "pushed " & s & vbNewLine
End If
End Function
Public Function Listing() As String
Dim S As String = String.Empty
If Prev.Count > 0 Then
S &= "Prev= " & Enumerate(Prev) & vbNewLine
End If
If Current.Count > 0 Then
S &= "Current= " & Enumerate(Current) & vbNewLine
End If
Return S
End Function
Private Function Enumerate(T1 As List(Of T)) As String
Dim s As String = String.Empty
For I = 0 To T1.Count - 1
s &= T1.Item(I).ToString & csComa
Next
Return s
End Function
Public Function Compare(T1 As List(Of T)) As String
Dim s As String = Enumerate(T1)
If ListsSame(Current, T1) Then
s &= "is same as current! (" & Enumerate(Current) & ")"
ElseIf ListsSame(Prev, T1) Then
s &= "is same as Previous! (" & Enumerate(Prev) & ")"
Else
s &= "does not match!"
End If
Return s & vbNewLine
End Function
Private Function ListsSame(T1 As List(Of T), T2 As List(Of T)) As Boolean
Dim ok As Boolean = False
If T1.Count > T2.Count Then
ok = T1.Except(T2).Any
Else
ok = T2.Except(T1).Any
End If
Return Not ok
End Function
End Class
我不得不通過在列表進行迭代「添加」例程作爲平等操作者將創建到的電流值的參考,而不是複製內容
測試代碼:
Dim t1 As New TestListClass(Of Integer), l1 As New List(Of Integer), t2 As New TestListClass(Of String), l2 As New List(Of String)
Dim x1() As Integer = {1, 2, 3, 4, 5}
l1.AddRange(x1)
Me.txtResult.Text = t1.add(l1)
Dim y1() As Integer = {6, 7, 8}
Dim l11 As New List(Of Integer)
l11.AddRange(y1)
Me.txtResult.Text &= t1.add(l11)
Me.txtResult.Text &= t1.Listing
Me.txtResult.Text &= t1.Compare(l1)
Me.txtResult.Text &= t1.Compare(l11)
l11.Add(9)
Me.txtResult.Text &= t1.Compare(l11)
Dim x2() As String = {"10", "20", "30"}
l2.AddRange(x2)
Me.txtResult.Text &= t2.add(l2)
Dim y2() As String = {"a", "b", "c"}
Dim l22 As New List(Of String)
l22.AddRange(y2)
Me.txtResult.Text &= t2.add(l22)
Me.txtResult.Text &= t2.Listing
Me.txtResult.Text &= t2.Compare(l2)
Me.txtResult.Text &= t2.Compare(l22)
l22.Add("d")
Me.txtResult.Text &= t2.Compare(l22)
輸出:
created System.Int32
pushed System.Int32
Prev= 1, 2, 3, 4, 5,
Current= 6, 7, 8,
1, 2, 3, 4, 5, is same as Previous! (1, 2, 3, 4, 5,)
6, 7, 8, is same as current! (6, 7, 8,)
6, 7, 8, 9, does not match!
created System.String
pushed System.String
Prev= 10, 20, 30,
Current= a, b, c,
10, 20, 30, is same as Previous! (10, 20, 30,)
a, b, c, is same as current! (a, b, c,)
a, b, c, d, does not match!
接着我打算當將selectedItem或的SelectedValue作爲基本變量被傳遞到分割基於gettype.IsgenericType的操作。
我猜這個問題是'SelectedIndices'是一個只讀屬性。 – LarsTech
我過去所做的是基於控制類型的撤銷,而不是撤消 - 在某些情況下,有不止一件事要注意。列表控件是一種痛苦 - 因爲你不能直接設置集合屬性,所以你可以循環'SelectedObjectCollection'並將每個項目的哈希碼存儲在列表中。 – Plutonix