How do I preload a form for faster display with VB 2005

Is there a way to preload a form without displaying it.  I want to shorten the response time between when a button is clicked and the form is displayed.

Tom
ThomasBoardmanAsked:
Who is Participating?
 
Mike TomlinsonConnect With a Mentor Middle School Assistant TeacherCommented:
Then you have to position it manually...something like:

    ...
    CenterForm(f1)
    ...

    Private Sub CenterForm(ByVal frm As Form)
        If Not (frm Is Nothing) Then
            frm.StartPosition = FormStartPosition.Manual
            frm.Location = New Point(Screen.PrimaryScreen.Bounds.Width / 2 - frm.Width / 2, Screen.PrimaryScreen.Bounds.Height / 2 - frm.Height / 2)
            frm.ShowInTaskbar = True
            frm.Show()
        End If
    End Sub
0
 
Éric MoreauSenior .Net ConsultantCommented:
you need to create an instance of it:
dim x as new form1

as long as you don't call "x.show", it won't be shown
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Declare a global public variable in a module:

    Public f1 As New Form1

Then you can use "f1" to show Form1.

    f1.Show()

This will not, however, speed up any code that may be present in the Form_Load() event...

I don't know how much speed gain you would see...it really depends on the complexity of the form and what it does...
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
ThomasBoardmanAuthor Commented:
Thanks for the responses.  I guess I was hoping for a way to run the form_load () event and not show the form until later by toggling the form's Visible property.  Any suggestions?
0
 
Éric MoreauSenior .Net ConsultantCommented:
Isn't creating an instance (dim x as NEW form1) calls the Load event?
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
No silly...

Creating an instance of a Form with the new keyword runs the CONSTRUCTOR matching the parameters you used.

The Load event is not run until the form is SHOWN with either Show() or ShowDialog().
0
 
yokkuiCommented:
An event shouldn't be fired when it's not supposed to be fired. That's my opinion. Why don't you take the code out of the form_load() event, put it into another method and call it instead?

Nonetheless, if you really really want the event to fire without showing the form (which I don't suggest), you may override the constructor (New method) of your form and make it fire the event, provided you see any point in doing that. :(

0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
You shouldn't "run" the Load() event code from the Constructor though since the controls may not have completely been instantiated, have handles and be ready for interaction...

Code in the Load() event often preloads items into controls, adds controls to the form or modifies existing controls...all things that assume the Form is ready for this...and this may not be the case when the Constructor is run after simply instantiating a Form with the new keyword.
0
 
ThomasBoardmanAuthor Commented:
Thanks IdleMind

If I understand what you're saying, the only way to safely load a form into memory is using Show() or ShowDialog()?  If that's true, is there a way to do so without seeing the form until later.  For example. maybe load the form behind my Main Form and then immediately hiding the form.  My guess, however, would be that you would momentarily see the form during the time it has focus.
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Here is one possibility...it loads the form "off the screen" and then hides it:

        Dim f1 As New Form1
        f1.StartPosition = FormStartPosition.Manual
        f1.ShowInTaskbar = False
        f1.Location = New Point(Screen.PrimaryScreen.Bounds.Right + 25, 0)
        f1.Show()
        f1.Hide()
0
 
ThomasBoardmanAuthor Commented:
Thanks for the suggestion, Idle Mind.  I do have one last question.  When I want to unhide the form, what's the best way to move the form from 'off the screen' to center screen?

Tom
0
 
ThomasBoardmanAuthor Commented:
I think I'm good to go, Idle Mind.  Thanks for your help!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.