2017-02-06 62 views
0

我正在爲一些自定義控件創建泛型Undoclass,並在嘗試實現多選列表框的撤消時陷入困境。Listbox SelectedIndexCollection與SelectedIndices

撤消類別:

Public Class UndoClass(Of T) 
    Private FirstValue As T 
    Private PrevValue As T 
    Private CurrentValue As T 
    Private HasValue As Boolean 
    Public Sub Add(ByVal Item As T) 
     If Not HasValue Then 
      FirstValue = Item 
      PrevValue = Item 
      HasValue = True 
     ElseIf Not CurrentValue.Equals(Item) Then 
      If Not CurrentValue.Equals(FirstValue) Then PrevValue = CurrentValue 
     End If 
     CurrentValue = Item 
    End Sub 

和一些撤銷代碼。在自定義列表框類我說:

dim undoing as new UndoClass(Of SelectedIndexCollection) 

Protected Overrides Sub OnEnter(e As EventArgs) 
    undoing.add(me.SelectedIndices) 
    .... 
Protected Overrides Sub OnSelectedIndexChanged(e As EventArgs) 
    if me.SelectedIndex>=0 then undoing.add(me.selectedIndices) 
    .... 

我遇到的問題是,通過「項目」,沒有相同的屬性作爲源將selectedIndices屬性,因此CurrentValue.equals(項目)測試總是失敗。儘管vs helpfile明確指出selectedIndices是一個「ListBox.SelectedIndexCollection,它包含了控件中當前選定項目的索引」,但我這樣做的方式並不起作用(它適用於所有其他控件,我只是通過它們的.text值作爲字符串或.Checked值作爲布爾等...)。

我做錯了什麼?

+0

我猜這個問題是'SelectedIndices'是一個只讀屬性。 – LarsTech

+0

我過去所做的是基於控制類型的撤銷,而不是撤消 - 在某些情況下,有不止一件事要注意。列表控件是一種痛苦 - 因爲你不能直接設置集合屬性,所以你可以循環'SelectedObjectCollection'並將每個項目的哈希碼存儲在列表中。 – Plutonix

回答

0

我想我找到了一種方法來解決它(只是一個測試程序,所以我用函數返回的測試信息,而不是方法和短變量名):

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的操作。