Discussion:
How to tell when form has fully loaded
(too old to reply)
Giles
2009-03-17 10:54:41 UTC
Permalink
Is there an event that occurs when a VB form has fully loaded and displayed?
(like the onload event in javascript)
Thanks
Rick Rothstein
2009-03-17 12:18:07 UTC
Permalink
If I had to guess, I would say the form's Activate event would tell you
that. Put this in the (General)(Declarations) section of the form's code
window...

Dim InitialLoad As Boolean

Put this in the form's Load event...

InitialLoad = "True"

And use this code layout for the form's Activate event...

Private Sub Form_Activate()
If InitialLoad Then
InitialLoad = False
'
' Put the code you want to run after the form
' loads and displays for the first time here.
'
End If
'
' Put your regular Activate event code here
'
End Sub

I would think the above layout should do what you want.
--
Rick (MVP - Excel)
Post by Giles
Is there an event that occurs when a VB form has fully loaded and
displayed? (like the onload event in javascript)
Thanks
-mhd
2009-03-17 14:45:07 UTC
Permalink
Post by Rick Rothstein
If I had to guess, I would say the form's Activate event would tell you
that. Put this in the (General)(Declarations) section of the form's code
window...
Dim InitialLoad As Boolean
Put this in the form's Load event...
InitialLoad = "True"
And use this code layout for the form's Activate event...
Private Sub Form_Activate()
If InitialLoad Then
InitialLoad = False
'
' Put the code you want to run after the form
' loads and displays for the first time here.
'
End If
'
' Put your regular Activate event code here
'
End Sub
I would think the above layout should do what you want.
If you have a Show command in your form load code then the Activate event will fire before
the rest of the form Load code is finished.

-mhd
Jeff Johnson
2009-03-17 15:22:11 UTC
Permalink
Post by Giles
Is there an event that occurs when a VB form has fully loaded and displayed?
For some crazy reason, the Resize event does. I usually do something like
this:

Private Sub Form_Resize()
Static fAlreadyDisplayed As Boolean

If Not fAlreadyDisplayed Then
' Do all your first-time-the-form-was-displayed stuff here

fAlreadyDisplayed = true
End If
End Sub
Nobody
2009-03-17 15:37:08 UTC
Permalink
Post by Jeff Johnson
Post by Giles
Is there an event that occurs when a VB form has fully loaded and displayed?
For some crazy reason, the Resize event does. I usually do something like
Private Sub Form_Resize()
Static fAlreadyDisplayed As Boolean
If Not fAlreadyDisplayed Then
' Do all your first-time-the-form-was-displayed stuff here
fAlreadyDisplayed = true
End If
End Sub
I use that method too. It's documented in the help: "Resize Event: Occurs
when an object is first displayed or when the window state of an object
changes."
Karl E. Peterson
2009-03-17 17:45:51 UTC
Permalink
Post by Nobody
Post by Jeff Johnson
Post by Giles
Is there an event that occurs when a VB form has fully loaded and displayed?
For some crazy reason, the Resize event does. I usually do something like
Private Sub Form_Resize()
Static fAlreadyDisplayed As Boolean
If Not fAlreadyDisplayed Then
' Do all your first-time-the-form-was-displayed stuff here
fAlreadyDisplayed = true
End If
End Sub
I use that method too. It's documented in the help: "Resize Event: Occurs
when an object is first displayed or when the window state of an object
changes."
Yeah, but as 'mhd' pointed out, I don't think this is the answer to "fully loaded"
either, in cases where a Me.Show is used in Form_Load. (Bad form, so to speak,
IMO.) If you're going to do that, it's really up to you (generic "you") to trigger
whatever you need to as the last line in Form_Load, because you have already futzed
up the entire scheme of things.
--
.NET: It's About Trust!
http://vfred.mvps.org
Vinchenzo vinç
2009-03-17 17:57:04 UTC
Permalink
Post by Giles
Is there an event that occurs when a VB form has fully loaded and
displayed? (like the onload event in javascript)
No there isn't.

If the Form is explicitly shown and any routine calls 'DoEvents' before
the 'Form_Load' ends, the Form will be fully loaded and displayed after the
last sentence of the 'Form_Load' event. Otherwise it will be fully loaded
and displayed at the first 'Form_Activate' notification:


'*****************
Public Event LoadComplete(ByVal CompletedOn As LoadCompleteConstants)

Public Enum LoadCompleteConstants
OnActivate
OnLoad
End Enum

Private m_Loaded As Boolean
Private m_Activated As Boolean

Private Sub Form_Activate()
If m_Activated = False Then
If m_Loaded = True Then RaiseEvent LoadComplete(OnActivate)
m_Activated = True
End If
'
'···
End Sub

Private Sub Form_Load()
'···
'
If m_Activated = True Then RaiseEvent LoadComplete(OnLoad)
m_Loaded = True
End Sub
'*****************
--
Regards
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
( ! ) http://groups.google.com/group/microsoft.public.vb.general.discussion
( i ) http://www.microsoft.com/communities/conduct/default.mspx
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Loading...