?
Solved

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

Posted on 2006-05-10
12
Medium Priority
?
561 Views
Last Modified: 2011-08-18
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
0
Comment
Question by:ThomasBoardman
  • 5
  • 4
  • 2
  • +1
12 Comments
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 16651068
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
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 16651075
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
 

Author Comment

by:ThomasBoardman
ID: 16651292
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 70

Expert Comment

by:Éric Moreau
ID: 16654659
Isn't creating an instance (dim x as NEW form1) calls the Load event?
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 16654793
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
 
LVL 3

Expert Comment

by:yokkui
ID: 16655692
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
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 16657876
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
 

Author Comment

by:ThomasBoardman
ID: 16660219
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
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 16661118
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
 

Author Comment

by:ThomasBoardman
ID: 16668626
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
 
LVL 86

Accepted Solution

by:
Mike Tomlinson earned 800 total points
ID: 16668922
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
 

Author Comment

by:ThomasBoardman
ID: 16669730
I think I'm good to go, Idle Mind.  Thanks for your help!
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
When you discover the power of the R programming language, you are going to wonder how you ever lived without it! Learn why the language merits a place in your programming arsenal.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
Simple Linear Regression

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question