0

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
  • Assuming that policies is the name of your form have you simply tried Dim ce as New Policies (without the .main) and of course ensured that policies has a proper constructor. – Dom Sinclair Jun 26 '15 at 06:50
  • I need to dim as Policies.Main as this is one of the classes witih Policies. What do you mean "Proper Constructor" – Lord Nodral III Jun 26 '15 at 06:52
  • Main might well be one of the classes in policies that you want to use, but unless you have an actual New instance of policies you'll never get access to main. – Dom Sinclair Jun 26 '15 at 06:53
  • Main is loading and appearing in the container, so I know this part is running correctly. However, it never accesses the load event. All other functionality is working. – Lord Nodral III Jun 26 '15 at 06:56
  • I'm getting confused here..Is policies the name of the form you're trying to load as a child , or is Main? You need to be clear about which is your parent and child. But for arguments sake its sounds as if you need something like this in your parent form. Dim ce as New child, ce.show(), If the child has a load event defined in it's code that should be called automatically when it is shown, but you must instantiate the child first. – Dom Sinclair Jun 26 '15 at 07:02
  • You'll have to bear with my explanation here as I'm not 100% sure on the terminology for each section. Policies is a VS solution containing several individual classes, of which main is one of them. So I do Dim ce as New Policies.Main. ce.MdiParent = Me. ce.show(). This works in many places throughout our systems, and I am lost as to why this one is not working. – Lord Nodral III Jun 26 '15 at 07:08

1 Answers1

1

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.

Dom Sinclair
  • 2,458
  • 1
  • 30
  • 47
  • I have used this principal in my existing code, and the messagebox never appears, indicating that the load event of the child is never triggered – Lord Nodral III Jun 26 '15 at 08:26
  • Can you actually add the full code of your parent, and the child you're trying to call as an edit to your question. It would help considerably in trying to figure out what's not working. – Dom Sinclair Jun 26 '15 at 09:00
  • I've removed all references in the parent, and readded them all, and it now seems to be working as expected. All very bizarre. Thanks for your help anyway – Lord Nodral III Jun 26 '15 at 09:19