Results 1 to 2 of 2

Thread: Form_Initialize & Form_Load!

  1. #1

    Thread Starter
    Frenzied Member arpan_de's Avatar
    Join Date
    Oct 2005
    Location
    Mumbai, India
    Posts
    1,394

    Question Form_Initialize & Form_Load!

    What's the difference between Form_Initialize & Form_Load?

    Of course, Form_Initialize gets fired first after which Form_Load fires. Are there any other differences between the 2 event functions?


    ARPAN

    IF YOU HAVE AN APPLE & I HAVE AN APPLE AND WE EXCHANGE THE APPLES, THEN YOU & I WILL STILL HAVE ONE APPLE BUT IF YOU HAVE AN IDEA & I HAVE AN IDEA AND WE EXCHANGE OUR IDEAS, THEN EACH OF US WILL HAVE TWO IDEAS!

    NOTHING IS IMPOSSIBLE IN THIS WORLD.....EVEN THE WORD IMPOSSIBLE SAYS I'M POSSIBLE!

    PRACTICE MAKES A MAN PERFECT BUT NOBODY IS PERFECT; SO WHY PRACTICE?

  2. #2
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,431

    Re: Form_Initialize & Form_Load!

    States a Visual Basic Form Passes Through
    A Visual Basic form normally passes through four states in its lifetime:

    Created, but not loaded.


    Loaded, but not shown.


    Shown.


    Memory and resources completely reclaimed.
    There's a fifth state a form can get into under certain circumstances: Unloaded and unreferenced while a control is still referenced.

    This topic describes these states, and the transitions between them.

    Created, But Not Loaded
    The beginning of this state is marked by the Initialize event. Code you place in the Form_Initialize event procedure is therefore the first code that gets executed when a form is created.

    In this state, the form exists as an object, but it has no window. None of its controls exist yet. A form always passes through this state, although its stay there may be brief.

    For example, if you execute Form1.Show, the form will be created, and Form_Initialize will execute; as soon as Form_Initialize is complete, the form will be loaded, which is the next state.

    The same thing happens if you specify a form as your Startup Object, on the General tab of the Project Properties dialog box (which is available from the Project menu). A form specified as the Startup Object is created as soon as the project starts, and is then immediately loaded and shown.

    Note You can cause your form to load from within Form_Initialize, by calling its Show method or by invoking its built-in properties and methods, as described below.

    Remaining Created, But Not Loaded
    By contrast, the following code creates an instance of Form1 without advancing the form to the loaded state:

    Dim frm As Form1
    Set frm = New Form1

    Once Form_Initialize has ended, the only procedures you can execute without forcing the form to load are Sub, Function, and Property procedures you've added to the form's code window. For example, you might add the following method to Form1:

    Public Sub ANewMethod()
    Debug.Print "Executing ANewMethod"
    End Sub

    You could call this method using the variable frm (that is, frm.ANewMethod) without forcing the form on to the next state. In similar fashion, you could call ANewMethod in order to create the form:

    Dim frm As New Form1
    frm.ANewMethod

    Because frm is declared As New, the form is not created until the first time the variable is used in code — in this case, when ANewMethod is invoked. After the code above is executed, the form remains created, but not loaded.

    Note Executing Form1.ANewMethod, without declaring a form variable, has the same effect as the example above. As explained in "Customizing Form Classes," Visual Basic creates a hidden global variable for each form class. This variable has the same name as the class; it's as though Visual Basic had declared Public Form1 As New Form1.

    You can execute as many custom properties and methods as you like without forcing the form to load. However, the moment you access one of the form's built-in properties, or any control on the form, the form enters the next state.

    Note You may find it helpful to think of a form as having two parts, a code part and a visual part. Before the form is loaded, only the code part is in memory. You can call as many procedures as you like in the code part without loading the visual part of the form.

    The Only State All Forms Pass Through
    Created, But Not Loaded is the only state all forms pass through. If the variable frm in the examples above is set to Nothing, as shown here, the form will be destroyed before entering the next state:

    Dim frm As New Form1
    frm.ANewMethod
    Set frm = Nothing ' Form is destroyed.

    A form used in this fashion is no better than a class module, so the vast majority of forms pass on to the next state.

    Loaded, But Not Shown
    The event that marks the beginning of this state is the familiar Load event. Code you place in the Form_Load event procedure is executed as soon as the form enters the loaded state.

    When the Form_Load event procedure begins, the controls on the form have all been created and loaded, and the form has a window — complete with window handle (hWnd) and device context (hDC) — although that window has not yet been shown.

    Any form that becomes visible must first be loaded.

    Many forms pass automatically from the Created, But Not Loaded state into the Loaded, but Not Shown state. A form will be loaded automatically if:

    The form has been specified as the Startup Object, on the General tab of the Project Properties dialog box.


    The Show method is the first property or method of the form to be invoked, as for example Form1.Show.


    The first property or method of the form to be invoked is one of the form's built-in members, as for example the Move method.
    Note This case includes any controls on the form, because each control defines a property of the form; that is, in order to access the Caption property of Command1, you must go through the form's Command1 property: Command1.Caption.

    The Load statement is used to load the form, without first using New or As New to create the form, as described earlier.
    Forms That Are Never Shown
    In the first two cases listed above, the form will continue directly on to the visible state, as soon as Form_Load completes. In the last two cases, the form will remain loaded, but not shown.

    It has long been common coding practice in Visual Basic to load a form but never show it. This might be done for several reasons:

    To use the Timer control to generate timed events.


    To use controls for their functionality, rather than their user interface — for example, for serial communications or access to the file system.


    To execute DDE transactions.
    Note With the Professional or Enterprise edition, you can create ActiveX components (formerly called OLE servers), which are often better at providing code-only functionality than controls are. See Creating ActiveX Components in the Component Tools Guide.

    Always Coming Home
    Forms return from the visible state to the loaded state whenever they're hidden. Returning to the loaded state does not re-execute the Load event, however. Form_Load is executed only once in a form's life.

    Shown
    Once a form becomes visible, the user can interact with it. Thereafter, the form may be hidden and shown as many times as you like before finally being unloaded.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width