0
votes

I have 3 forms

  1. MDI parent for TCP client
  2. MDI parent for serial
  3. The main menu form

I want to set the main menu form as the startupform then I want the 2 MDI parent to be loaded on the background. I don't want to show them, but I want to load them so I can trigger the on_load event of those 2 mdi parent which is to create serial/client child and connect automatically.

I've tried, but it didn't work.

Private Sub frmMenu_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    frmTcpMDI.show
    frmSerialMDI.show
End Sub

I even tried adding a timer then starting it upon the show event of my startup form, the tick event is to open my frmTcpMDI and frmSerialMDI, still, it is not working.

1
Maybe if you don't ever want to show them, they don't need to be Forms but just classes. I don't exactly understand what you want to achieve, can't you start the connection in the Sub New() methods of the forms ?Martin Verjans
What I want is just to start their process at startup, there are times that the user needs to see what's inside those MDI parents so I can't change them to just classes because they also contain informations that my users want to see.Cary Bondoc
What can somewhat work is to do a normal load (New() then Show()) and in the Load event of the forms you call Me.Hide()... But it's not good practice I think...Martin Verjans
Hmm, could you explain it more? Maybe as an answer, I just need to temporarily solve this problem.Cary Bondoc

1 Answers

0
votes

Okay basically, if I understand what you want, you need a way to load your forms but not display them.

You want to run some code when the Load event occurs. However, as stated here by Microsoft :

Occurs before the control becomes visible for the first time.

Which basically screws you up...

So there is a few workaround for that.

Option 1 : You decide to run your code in the Sub New()

If you put all the code you want to run in the Sub New() of your child forms, you don't need to call Form.Show(), you just need to create the forms, and your code is running. When you actually need to display the form you call the Show() method and it's done.

Option 2 : You can't run your code in the Sub New()

If, for some reason, you can't run your piece of code in the Sub New() method, you still can do it with the load event. Just hide the form after showing it... Looks silly but will work.

First notice, still from Microsoft :

The Load event occurs when the handle for the UserControl is created. In some circumstances, this can cause the Load event to occur more than one time. For example, the Load event occurs when the UserControl is loaded, and again if the handle is recreated.

Which mean you have to be carefull to run your code only once...

Then, from your parent :

Dim myNewForm = new frmTCPMdi()
myNewForm.Show()'will call the Load event
myNewForm.Hide()'will hide the form, so it is loaded but invisible...

Honnestly, I think Option 1 looks better, but sometimes and for some reason you can't always go the easy way...