Question Populating combobox on tab control

dannyseager

New member
Joined
May 12, 2012
Messages
2
Programming Experience
5-10
I have a simple function in a class that I use to populate combo boxes. I have many combo boxes in the application that read data from the same table (a lookup table).

VB.NET:
Public Sub LookupBox(frm As Form, cbo As String, section As String)

        Dim db As New db

        Dim Connstring = "" ' removed for posting

        Dim SQL As String = "SELECT 0 as LookupValue,'' as LookupText UNION SELECT LookupValue,LookupText FROM TblLookups WHERE active=1 And LookupSection='Title'"

        Dim myConnection As New SqlConnection(Connstring)
        myConnection.Open()

        Dim da As New SqlDataAdapter(SQL, myConnection)
        Dim ds As New DataSet
        da.Fill(ds, "Lookup")

        Dim cb As ComboBox
        cb = CType(frm.Controls(cbo), ComboBox)

        With cb
            .DataSource = ds.Tables("Lookup")
            .DisplayMember = "LookupText"
            .ValueMember = "LookupValue"
            .SelectedIndex = 0
        End With

        myConnection.Close()
        myConnection.Dispose()

    End Sub

All this works fine when the combo box is flat on the form... but when it is on a tab control it can't find the combo box.

I tried making it check controls within controls but not much luck

I changed

VB.NET:
cb = CType(frm.Controls(cbo), ComboBox)

for

VB.NET:
   If Len(ParentControl & "") = 0 Then
            ' flat on form
            cb = CType(frm.Controls(cbo), ComboBox)
        Else
            If Len(ParentControl2 & "") = 0 Then
                cb = CType(frm.Controls(ParentControl).Controls(cbo), ComboBox)
            Else
                cb = CType(frm.Controls(ParentControl).Controls(ParentControl2).Controls(cbo), ComboBox)
            End If
        End If

and passing the 2 optional parameters "ParentControl" and "ParentControl2" bur I'm obviously missing something

I've setup 2 optional parameters incase I need to populate a combo box nested in a tab control that is in a tab control.

Any help welcome.

Danny
 
Back
Top