ComboBox没有通过字典学习填充独特的字段值

时间:2015-06-09 13:41:35

标签: vb.net dictionary combobox

以下代码驻留在用户控件中,其属性已从主程序成功填充(设置)。出于某种原因,当在ComboBox1中选择不同的项目时,ComboBox2中的项目不会更改 - 尽管myarray中的数据在各种字段和记录中是不同的。问题似乎出现在Combobox1的选定索引中,因为Combobox2中的项目永远不会改变,但数据不同。因此,在尝试填充ComboBox2项时,从Combobox 1绘制的索引不会更改。有什么建议吗?

Public Class filter
    Public Property numfields As Integer
    Public Property fieldnames As String()
    Public Property numrecs As Integer
    Public Property myarray As Object(,)
    Sub filter_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        For i As Integer = 1 To numfields
            ComboBox1.Items.Add(fieldnames(i))
        Next
    End Sub
    Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
        ComboBox2.Items.Clear()
        Dim index As Integer = ComboBox1.SelectedIndex + 1 'This is the input variable that was selected
        Dim dicfilter As New Dictionary(Of String, List(Of String))
        For i = 1 To numrecs 'input word is not known, so learn it (increment known word array by 1 and add word)
            If dicfilter.ContainsKey(myarray(i - 1, index - 1).ToString) = False Then
                Dim myValues As New List(Of String)
                myValues.Add("")
                dicfilter.Add(myarray(i - 1, index - 1).ToString, myValues)
                ComboBox2.Items.Add(myarray(i - 1, index - 1).ToString)
            End If
        Next
    End Sub
End Class

2 个答案:

答案 0 :(得分:1)

解决。需要清除字典,并且还需要将其定义移动到公共,如下面的代码所示:

Public Class filter
    Public Property numfields As Integer
    Public Property fieldnames As String()
    Public Property numrecs As Integer
    Public Property myarray As Object(,)
    Public dicfilter As New Dictionary(Of String, List(Of String))
    Sub filter_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        For i As Integer = 1 To numfields
            ComboBox1.Items.Add(fieldnames(i))
        Next
    End Sub
    Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
        ComboBox2.Items.Clear()
        dicfilter.clear
        Dim index As Integer = ComboBox1.SelectedIndex + 1 'This is the input variable that was selected
        For i = 1 To numrecs 'input word is not known, so learn it (increment known word array by 1 and add word)
            If dicfilter.ContainsKey(myarray(i - 1, index - 1).ToString) = False Then
                Dim myValues As New List(Of String)
                myValues.Add("")
                dicfilter.Add(myarray(i - 1, index - 1).ToString, myValues)
                ComboBox2.Items.Add(myarray(i - 1, index - 1).ToString)
            End If
        Next
    End Sub
End Class

答案 1 :(得分:-1)

为什么要在1和0时启动指数

此外,在下面的这一行中,NUMRECS代表什么?

For i = 1 To numrecs     'input word is not known, so learn it (increment known word array by 1 and add word)

如果NUMRECS为0,那么你的LOOP将永远不会执行,它会直接跳到NEXT然后再转到END SUB!