Custom ListBox not being added to form

ent_ttu

Member
Joined
Jun 17, 2008
Messages
14
Programming Experience
1-3
I'm new to VB, and am having trouble with yet another thing... I'm using VB.net 2005 and MS access.

Problem overview: I have a list of people in a database table that I want to put into a listbox, with a pre-determined person (the current value) already highlighted. I need to do this on several separate forms, so I thought I'd create a class for the custom listbox.

The class (rpListBox) seems to be working OK; if I tell the class to add the control to a specific form (frmEdit), it appears on the form.

If I comment out the line in the class that tells it to add to the form, and add that line to the code in the form itself, nothing appears and I don't get an error message.

Here's the code for the class I wrote:
VB.NET:
    Public Sub New(ByVal responsibleID)
        con.ConnectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source = C:\barcode_inventory.mdb"
        con.Open()

        rpSQL = "select person_id from person where status = 'a' order by last_name"

        da = New OleDb.OleDbDataAdapter(rpSQL, con)
        da.Fill(ds, "ppl")

        MaxRows = ds.Tables("ppl").Rows.Count
        inc = 0

        Do While inc < MaxRows
            Dim resp As New responsibleParty(ds.Tables("ppl").Rows(inc).Item("person_id"))

            lstRespParty.Items.Add(resp.rpLastNameFirst)

            If (resp.rpId = invNo.responsibleID) Then
                lstRespParty.SelectedItem = resp.rpLastNameFirst
            End If

            inc = inc + 1
        Loop

        'this line works in this class
        frmEdit.Controls.Add(lstRespParty)

    End Sub

Here's the code snippet from frmEdit that doesn't work:
VB.NET:
        Dim lstRespParty As New rpListBox(invNo.responsibility)
        'this line doesn't do anything
        Me.Controls.Add(lstRespParty)

I don't want to hard-code the form name in the rpListBox class, because I want to use it from different forms.

I'm sure it's something simple, and I'd appreciate any help or suggestions. Thanks!
 
Try passing the parent form to the constructor sub (the New() sub):
VB.NET:
Public Sub New(ByVal responsibleID, ByVal ParentForm As Form)
'......
ParentForm.Controls.Add(lstRespParty)

End Sub
 
Thanks for the quick response!

I had previously tried something similar, without success.

I changed my rpListBox code as follows:
VB.NET:
Public Sub New(ByVal responsibleID, ByVal ParentForm As Form)

And changed my frmEdit code to:
VB.NET:
Dim lstRespParty As New rpListBox(invNo.responsibleID, frmEdit)

Now I'm getting "All parameters must be explicitly typed if any are." in the rpListBox code. I've tried googling the error, without much success.

Thanks again for the response!
 
Resolved

OK; I was able to solve that error by changing my code in rpListBox to:

VB.NET:
 Public Sub New(ByVal responsibleID As String, ByVal ParentForm As Form)


Then, I had to change my code in frmEdit to:
VB.NET:
Dim lstRespParty As New rpListBox(invNo.responsibleID, Me)

After that, it finally worked!

Thanks so much for the assistance. Now, on to the next problem...
 
Back
Top