Load Event of MDi child form not firing?

1.7k views Asked by At

I have a simple form which calls an external class containing another form in a vb.NET application.

The 2 forms are set up as an MDi parent and child.

Does anyone know why when I call MDIChild.show() in the code of the parent, the load event does not fire in the child form??

Parent Code:

 Dim ce As New Policies.Main
    ce.MdiParent = Me
    ce.Show()

Child Code

Public Sub Main_Load(sender As Object, e As System.EventArgs) Handles MyBase.Load
'Do some stuff in load event
End Sub
1

There are 1 answers

3
Dom Sinclair On

Right Following on from the comments above. Open up visual studio and create a simple Winforms project. It will be created with a default instance of Form1.

In the solution explorer right click on the solution and select add and from the menu that appear select Windows form. A new windows form will be created with a default name of Form2.

We are going to treat form 1 as our parent class and form 2 as our child.

Go back to form one and drag a button onto it from the toolbox. Double click on the button once its on the form to open up its default button click handler.

Add the following code:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Me.IsMdiContainer = True  'we need this so that Form1 can act as a container for other forms
        Dim frm As New Form2
        With frm
            .MdiParent = Me
            .Show()
        End With

    End Sub

Now return to form2. Doubleclick on it to bring up its default load event in the code editor. Add the following code.

Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        MessageBox.Show("Hi, I'm being shown from the load event of form2")
    End Sub

With that done press f5 to run this very simple ( and crude) example. Form1 will load. when you click the button a new instance of Form2 is created. Prior to the form being shown its load event is fired and that triggers te message box to display it's message. You do not need to call the load method directly.