ComboBox Not Being Filled With Unique Field Values Via Dictionary Learning

113 views Asked by At

The following code resides in a user control, whose properties are successfully filled (set) from the main program. For some reason, when different items are selected in ComboBox1, the items in ComboBox2 do not change -- although the data in myarray are different in various fields and records. The problem seems to be in the selected index from Combobox1, since the items in Combobox2 never change but the data are different. Therefore, the index drawn from Combobox 1 is not changing when trying to fill ComboBox2 items. Any suggestions?

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

There are 2 answers

1
Zeddy On

Why do you start your INDICES at 1 and NOT 0

Additionally in this line below, what does NUMRECS represent?

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

If NUMRECS is 0 then your LOOP will NEVER Execute and it will jump straight to the NEXT and then to END SUB!

0
AudioBubble On

Resolved. The dictionary needed to be cleared and its definition also needed to be moved to public, as in the following code:

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