继承Combobox的.NET控件 - Items.Clear不会清除项目列表

时间:2013-11-07 16:16:41

标签: .net vb.net combobox

我有一个继承ComboBox的类。

我有一个接收List(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

之后无法清除

0 个答案:

没有答案
相关问题