Results 1 to 2 of 2

Thread: Populating combobox on tab control

  1. #1
    dannyseager is offline VB.NET Forum Newbie
    .NET Framework
    .NET 4.0
    Join Date
    May 2012
    Posts
    2
    Reputation
    0

    Populating combobox on tab control

    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).

    Code:
    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

    Code:
    cb = CType(frm.Controls(cbo), ComboBox)
    for

    Code:
       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

  2. #2
    dannyseager is offline VB.NET Forum Newbie
    .NET Framework
    .NET 4.0
    Join Date
    May 2012
    Posts
    2
    Reputation
    0
    I was over complicating it... I just passed the cb as an argument and it works fine

    Problem solved

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Harvest time tracking