0

我有一个继承 ComboBox 的类。

我有一个属性,它接收一个 List(of String) 并将 Items 集合设置为相同的值。除了,当我清除项目列表时,它并不总是清除!

Set(ByVal value As System.Collections.Generic.List(Of String))
    MyBase.Items.Clear()
    If MyBase.Items.Count <> 0 Then
        'Shouldn't ever get here
        Throw New Exception
    End If

    For Each item As String In value
        If item Is Nothing Then
            Items.Add(String.Empty)
        Else
            Items.Add(item)
        End If
    Next
End Set

有人对可能发生的事情有任何想法吗?

编辑:

我更改了代码以查看变量中的内容:

Set(ByVal value As System.Collections.Generic.List(Of String))
    Debug.WriteLine("values passed to property: " & Join(value.ToArray, ", "))
    Dim itemvalues As String = String.Empty
    For Each item As String In Items
        itemvalues &= item & ", "
    Next
    Debug.WriteLine("items in combobox: " & itemvalues)
    Items.Clear()
    If Items.Count <> 0 Then
        'Shouldn't ever get here
        Throw New Exception
    End If

    For Each item As String In value
        If item Is Nothing Then
            Items.Add(String.Empty)
        Else
            Items.Add(item)
        End If
    Next
End Set

这就是我得到的:

values passed to property: a, c, b
items in combobox: 
values passed to property: a, c, b
items in combobox: a, c, b, 
values passed to property: a, c, b
items in combobox: 
values passed to property: a, c, b
items in combobox: a, c, b, 
values passed to property: a, c, b
items in combobox: a, c, b, 
values passed to property: a, c, b
items in combobox: a, c, b, 
values passed to property: a, c, b
items in combobox: a, c, b, 
values passed to property: a, c, b
items in combobox: a, c, b, 

最后一对debug.writeline后清除失败

4

0 回答 0