Re: Loading a form
- From: "MikeD" <nobody@xxxxxxxxxxx>
- Date: Fri, 5 Jan 2007 20:57:12 -0500
<James@xxxxxxx> wrote in message news:rjnsp2pddb8cbee7kll4r7hhpcnom3o3ku@xxxxxxxxxx
I have a form that takes a while to load (about 3 seconds). How do i
get it to load in background whiile a splash screen displays?
Show the splash screen first. Just don't show the splash screen modally. In your main form, I'd recommend moving all code in Form_Load code into a Public procedure (if you're not doing it that way already). Load the main form (don't show it yet) and call that procedure immediately after showing the splash screen. When that public procedure returns, unload the splash screen and show your main form. This is easier if you're using Sub Main as your startup object because you'd do all of this from there. Here's an example. Start a new Standard EXE project. Add 2 forms and keep default names (Form2 will be the splash screen). Move and size the forms however you want. You can add controls to them, but it's not necessary for this example. Add a standard code module (.bas file). Open Project Properties and make Sub Main the Startup Object (on the General tab). Copy and paste this code into the .bas file:
-----BEGIN CODE
Option Explicit
Private Sub Main()
Load Form1
Load Form2
Form2.Show vbModeless
DoEvents 'make sure splash screen is drawn before continuing
If Form1.Setup Then
Unload Form2
Form1.Show
Else
'Problem setting up main form; close app
Unload Form2
Unload Form1
End If
End Sub
-----END CODE
Copy and paste this code into Form1 (the main form)
-----BEGIN CODE
Option Explicit
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Public Function Setup() As Boolean
'Write all your initialization code here.
'I'm using the Sleep API function to simulate
'taking 3 seconds for all the initialization
'code to execute. Ideally, there should be no
'code AT ALL in the Form_Load event.
On Error GoTo EH
Sleep 3000
Setup = True
Exit Function
EH:
Setup = False
End Function
-----END CODE
Make sure you've written housekeeping or other clean up code as necessary, either in the Form_Unload events (your splash screen shouldn't have any clean up to do, though) or in the error handler of the Setup function as appropriate. If there's a problem in the Setup function, it should return False. This will allow the form to be properly unloaded in Sub Main.
--
Mike
Microsoft MVP Visual Basic
.
- Follow-Ups:
- Re: Loading a form
- From: MikeD
- Re: Loading a form
- References:
- Loading a form
- From: James
- Loading a form
- Prev by Date: Re: Another Q for Vista compatibility
- Next by Date: Re: Loading a form
- Previous by thread: Loading a form
- Next by thread: Re: Loading a form
- Index(es):
Relevant Pages
|