How to get frmMain to load before other dependent forms?

D

Dean Slindee

My project has a main form (frmMain, the startup object for the project) and
several other "child" forms that are painted within a large panel on
frmMain.

In each form's Form_Load event, a Weak Reference for that form is loaded
into a global Hash table, like this:

Dim wr As WeakReference = New WeakReference(Me, False)

If Not hashTable.Contains(cfrmMain) Then

hashTable.Add(cfrmMain, wr)

End If

I also have the following declares in frmMain, just after the designer code,
for each "child" form that gets painted inside the panel on frmMain:

Friend frmAdmin As New frmAdmin

Friend frmAppointment As New frmAppointment

Friend frmBilling As New frmBilling

Friend frmSystem As New frmSystem



Each child form writes to the status bar on frmMain in their Form_Load
event.

Problem is, the load events for the child forms are firing before

the load event for frmMain has put a reference into the hash table.



How can I get frmMain's Form_Load to fire completely before child Form_Load
events

fire? Or, how can I get an entry for frmMain into hashTable before child
Form_Load

events fire?



Above and beyond this specific problem, I would like to find an article that
explains in

great detail exactly what happens in sequence when a VB application is
launched.



Thanks,

Dean Slindee
 
T

Tian Min Huang

Hello Dean,

Thanks for your post. As I understand, the problem you are facing is that
your "child" forms' Load methods are called before that of the frmMain.
Please correct me if there is any misunderstand.

The Form.Load event occurs before a form is displayed for the first time.
So, generally speaking, the Load method of startup Form will be called
first. I am able to verify it by the following steps:

1. Create a WinForm application.

2. Add a new Form (Form2) to the project.

3. In the Load method of each form, add the following code to display a
message box.
MessageBox.Show("Form1_Load")
or
MessageBox.Show("Form2_Load")

4. Have the following declare in Form1 (the startup object):
Friend frmForm2 As New Form2

5. Execute the program and you will notice that Form2_Load() will not be
called until you explcitly call Form2.Show().

For further research, could you please tell me when you try to paint
"child" Forms to the frmMain? I recommend you paint them at the end of the
frmMain_Load method.

Hope this helps.

Regards,

HuangTM
Microsoft Online Partner Support
MCSE/MCSD

Get Secure! -- www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
A

Armin Zingler

It's not required to post three times. Sometimes it might take many hours to
get an answer. I'd suggest to wait at least one day before reposting.

(Concerning me, I skipped your posts as soon as I read "WeakReference"
because I've never dealt with it. Now I'll try...)
My project has a main form (frmMain, the startup object for the
project) and several other "child" forms that are painted within a
large panel on frmMain.

In each form's Form_Load event, a Weak Reference for that form is
loaded into a global Hash table, like this:

Dim wr As WeakReference = New WeakReference(Me, False)

If Not hashTable.Contains(cfrmMain) Then

hashTable.Add(cfrmMain, wr)

End If

I also have the following declares in frmMain, just after the
designer code, for each "child" form that gets painted inside the
panel on frmMain:

Friend frmAdmin As New frmAdmin

Friend frmAppointment As New frmAppointment

Friend frmBilling As New frmBilling

Friend frmSystem As New frmSystem



Each child form writes to the status bar on frmMain in their
Form_Load event.

Problem is, the load events for the child forms are firing before

the load event for frmMain has put a reference into the hash
table.



How can I get frmMain's Form_Load to fire completely before child
Form_Load events

fire? Or, how can I get an entry for frmMain into hashTable before
child Form_Load

events fire?

You are mixing the order. The order is
1. create form
2. load form
3. show form
4. child paints anything (resp. change the display)

I'd code it appropriately:

dim f as new frmmain
f.show 'loading is done implicitly
' just before the form is shown
frmmain.refresh
frmmain.loadchildforms 'not in form_load


Loadchildforms creates the child instances, so don't declare them "As /New/
frmAdmin"
Another improvement approach is probably to make the child raise events and
catch them in frmMain. In the event handler, frmMain can update it's own
status bar.

Above and beyond this specific problem, I would like to find an
article that explains in

great detail exactly what happens in sequence when a VB application
is launched.

Sub main is started. It is running in the one and only thread of the
application. The application quits as soon as all threads terminated. If you
have one thread only, the app ends when your main thread ends. To keep sub
main running, there must be a loop that keeps the thread and the app alive.
In Windows.Forms applications, sub main contains a message loop that
processes all windows messages (key presses, mouse movements,...). If you
write sub main on your own, it can look like this:

sub main
application.run(new form1)
end sub

application.run contains the message loop and it returns as soon as the
passed form has been closed. In this sub main you are very flexible, you can
write anything you want.

If you specify a Form as the startup object in the project properties, VB
implicitly creates (invisible) code for you. It looks like this:

sub main
application.run(new form1)
end sub

Surprise, it's the same as above, but this code is fixed. In the first case,
you are free to fill Sub main as you want.


--
Armin

How to quote and why:
http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html
 
D

Dean Slindee

Armin,
Thank you so much for your insightful reply. I spent all day reworking the
communications between the main form and it's many children, based on your
suggestions. I am pleased to say that everything is working fine now. Many
thanks for erasing some of the fuzzy areas in my thinking.

Your second suggestion of using RaiseEvents to send status messages from the
child forms to the main form's status bar is also appealing. I have not got
it working yet, but here is what I have so far:
In frmMain:
Private WithEvents frm As New frmChild

Private Sub frm_StatusMessage(ByVal strMessage As String) Handles
frm.StatusMessage

MessageBox.Show(strMessage, "RaiseEvent", MessageBoxButtons.OK,
MessageBoxIcon.Information)

End Sub

In frmChild:

Public Event StatusMessage(ByVal strStatus As String)

RaiseEvent StatusMessage("Houston, we have a problem")

Syntax is accepted, but the event is not trapped by frmMain. Any
suggestions?

Dean Slindee
 
A

Armin Zingler

Dean Slindee said:
I have not got it working yet, but here is what I have so far:
In frmMain:
Private WithEvents frm As New frmChild

Private Sub frm_StatusMessage(ByVal strMessage As String) Handles
frm.StatusMessage

MessageBox.Show(strMessage, "RaiseEvent", MessageBoxButtons.OK,
MessageBoxIcon.Information)

End Sub

In frmChild:

Public Event StatusMessage(ByVal strStatus As String)

RaiseEvent StatusMessage("Houston, we have a problem")

Syntax is accepted, but the event is not trapped by frmMain. Any
suggestions?

I guess you never show frm but another instance. Where is Raiseevent
located? Set a breakpoint at Raiseevent and look if it's reached.


--
Armin

How to quote and why:
http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html
 
T

Tian Min Huang

Hi Dean,

I reviewed your code snippet and it seems correct for the event handling.
The following MSDN arcticle describes it in details:

Raising Events and Responding to Events
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dndotnet/ht
ml/raiserespeven.asp

For further research, could you please tell me when you call RaiseEvent in
frmChild? Is it possible for you to post a simple reproducible project?

I look forward to hearing from you.

Have a nice day!

Regards,

HuangTM
Microsoft Online Partner Support
MCSE/MCSD

Get Secure! -- www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
D

Dean Slindee

Thanks for the link to the Microsoft web page. I built the example, which
eventually
led me to discover the solution to being able to get RaiseEvent working in
my project.
Thanks again.
Dean Slindee
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top