Controls, Hashtables & NullReferenceException

mike55

Contributor
Joined
Mar 26, 2004
Messages
727
Location
Ireland
Hi

I am using the following code:

Code:
   Private Sub ReloadSelectionCriteria(ByVal userSelection As Hashtable)
        For Each entry As DictionaryEntry In userSelection
            CType(Controls(entry.Key), ComboBox).SelectedItem = entry.Value
        Next
    End Sub

to loop through all the keys in a hash table. All the keys are the names of a valid combobox displayed on screen. What I am trying to do is to reflect the values chosen on the comboboxes on the parent screen through to the child screen. However, with the above code I am getting a NullReferenceException.

Any suggestions? I believe that the problem is that my code cannot find the control on the windows form.

Mike55.
 
Fixed my initial problem:
Code:
   Private Sub ReloadSelectionCriteria(ByVal userSelection As Hashtable, ByVal parent As Object)
        Dim myC() As Control

        For Each entry As DictionaryEntry In userSelection
            myC = Controls.Find(entry.Key.ToString, True)

            CType(myC(0), ComboBox).SelectedIndex = entry.Value
        Next
    End Sub

I now have an additional issue, the form that I am using is an instance of the parent form and both of them remain open on screen. When I cycle through the hashtable, the above code is simply looking at the parent form. How can I re-target it at the child form?

Mike55.
 
Solution:
Code:
 Private Sub ReloadSelectionCriteria(ByVal userSelection As Hashtable, ByVal parent As Object)
        Dim myC() As Control

        For Each entry As DictionaryEntry In userSelection
            myC = parent.Controls.Find(entry.Key.ToString, True)

            CType(myC(0), ComboBox).SelectedIndex = entry.Value
        Next
    End Sub
 
Back
Top