Question parent and child forms

viper5646

Active member
Joined
Jan 20, 2009
Messages
38
Location
Canada
Programming Experience
Beginner
Hi

I have a form with the list box that is loaded from an Access DB.
When I run this form alone it works OK.
But if I open it from a parent form the list box does not load.
Since this is my first App I wander if I have the wrong code in the parent Form.
Here is the code I have in the parent for to open the child form.

VB.NET:
Private Sub tbbModel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tbbModel.Click

        Dim ModelMDIChild As New ModelForm()
        ModelMDIChild.MdiParent = Me
        ModelMDIChild.Show()
    End Sub
Thank you
 
yes the form displays.
If I run the form that has the ListBox alone not from the parent form, the ListBox populates ok.
Any way here is the coed that populates the ListBox.
When I debug it I put a stop in this sub and the builderlist has values, so I'm assuming that it is not assigning it to the ModelForm.chklstBuilder.Items
Thanks
VB.NET:
 Private Sub BuilerLst()
      For Each builder In builderlist
      ModelForm.chklstBuilder.Items.Add(builder)
      Next
           End Sub
 
Think about it. Which form should you be trying to write to - ModelForm or ModelMDIChild ;)
 
ModelForm, unqualified, means the default ModelForm form instance, which is not the same instance as the one you created New.
 
I have tryed Inertiam way but when I try to use ModelMDIForm Insted of Modelform I get a not decleared error, because only Modelform existes not ModelMDIform.
Thaks John and Inertiam for helping
 
ModelMDIForm is not available because you declared it within tbbModel_Click subroutine, as opposed to declaring it Private (or Public) within the Parent form itself.

If you want to access the ModelMDIForm instance at any point after you declare it, you will have to store the reference in some other way so it can be addressed.
 
Don't know if this makes a difference or not.
But I neglected to mention that the code that is populating the ListBox in the modelfom is not written in the modelform but it is in a Module.
I also tried to make the ModelMDIForm in the Parent but it work. the Listbox is still not being populated.
Thanks all .
Hope to hear from you.
 
JohnH has already given you the answer. The code you are using to display the form is creating a new instance explicitly while the code you're using to populate the list is using the default instance, so you are working with two different ModelForm objects. The one you are displaying is not the one your are populating. If you want to populate the default instance then you must display the default instance too, i.e. this:
VB.NET:
        Dim ModelMDIChild As New ModelForm()
        ModelMDIChild.MdiParent = Me
        ModelMDIChild.Show()
becomes this:
VB.NET:
        ModelForm.MdiParent = Me
        ModelForm.Show()
That said, why is the code to populate the ListBox in a module and not in the form itself? While it's legal it really isn't the best design choice. If the data used to populate the list is stored in the module then the form should be getting that data and populating its own list. In that case it wouldn't have mattered whether you were using the default instance or not.

This is not your fault viper5646 but this is a fine example of why default instances are a bad idea. They were introduced to make it easier for beginners but it just makes it easier for them to make mistakes because they don't understand what a default instance is and how it differs from an explicit instance. If they didn't exist then this mixing and matching wouldn't be possible. Basically, if you're going to use default instances at all then you should use default instances exclusively. That means that you ALWAYS refer to a form through its class name and you NEVER use the New keyword to create a form. The only time you would break that rule is if you needed multiple instances of the same form class. Having said that, it's far better to not use default instances at all and just treat forms like any other objects, which everyone would have had to still be doing if default instances hadn't been introduced.
 
HI jmcilhinney

I have tried your code but it still does not populate the ListBox.
to answer your question the reason why I have the code in a module is because there other forms using the same code to populate a listbox, which are not being populated just like the modelform.
Do you have other options?
thanks
 
HI jmcilhinney

I have tried your code but it still does not populate the ListBox.
to answer your question the reason why I have the code in a module is because there other forms using the same code to populate a listbox, which are not being populated just like the modelform.
Do you have other options?
thanks
But the code you posted from your module SPECIFICALLY populates a ListBox on the default instance of the ModelForm class, so how can that possibly be used to populate ListBoxes on other forms?

If I had a dollar for every time someone said that they were using my code and it didn't work, when in actual fact they were not using my code, I would be living comfortably. Please, if you are using some code that doesn't work then post that code. Just telling us what you're doing is often not enough because what you think you're doing is not actually what you're doing, which is why it's not working. If you post the code then we KNOW exactly what you ARE doing and we can likely see the problem.
 
Here is the code

hope this helps

thank you for all your effort

VB.NET:
   Private Sub mnuModel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuModel.Click

        ModelForm.MdiParent = Me
        ModelForm.Show()

    End Sub
VB.NET:
Private Sub ModelForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        Builder.m_Builder()
        Me.txtModel.CharacterCasing = CharacterCasing.Upper
    End Sub
VB.NET:
Public Sub m_Builder()
        Try
            Dim mycon As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=c:\DEsign.accdb"
            Dim con As New OleDb.OleDbConnection(mycon)
            Dim comBuilder As New OleDb.OleDbCommand
            Dim combuilderID As New OleDb.OleDbCommand
            con.Open()

            comBuilder.CommandText = String.Format("select cmdbldr from cmdbuilder Order by cmdbldr")
            comBuilder.Connection = con
            Dim Dr_builder As OleDb.OleDbDataReader
            Dr_builder = comBuilder.ExecuteReader
            Dim x As Integer = 0
            While Dr_builder.Read
                builderlist.Add(Dr_builder.GetString(0))
                x = x + 1
            End While
            Dr_builder.Dispose()

            combuilderID.CommandText = String.Format("SELECT bldrid from cmdbuilder Order by cmdbldr")
            combuilderID.Connection = con
            Dim drbuilderID As OleDb.OleDbDataReader
            drbuilderID = combuilderID.ExecuteReader
            Dim xx As Integer = 0
            While drbuilderID.Read
                buildrID.Add(drbuilderID.GetInt32(0))
                xx = xx + 1
            End While

            drbuilderID.Dispose()
            con.Close()
            
        For Each id In buildrID            ModelForm.CmbBuilderID.Items.Add(id)
        Next

        For Each builder In builderlist
            DsgnForm.CmbBuilder.Items.Add(builder)
            ModelForm.chklstBuilder.Items.Add(builder)
        Next

        DsgnForm.CmbBuilder.Sorted = True
        
    End Sub

        Catch ex As Exception
            MessageBox.Show("ERROR  " & ex.Message)
        End Try

    End Sub

If we can not solve this I'll just have to write the Module code in the the form they belong to.
 
Think about this for a moment:

When you use a TableAdapter to Fill a DataTable, you pass the DataTable you want to Fill, to the TableAdapter:

myTA.Fill(myDT)


So, as a logical follow on, why don't you pass the listbox you want to fill, to the filling method, then the filling method can just fill whatever listbox is passed.

This way your design form can call Fill(Me.DesignListBox) and your Builder form can call Fill(Me.BuilderListBox) etc
 
Back
Top