Results 1 to 5 of 5

Thread: Initialize vs. Load

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jul 2000
    Location
    Boulder, Colorado
    Posts
    22

    Question

    When and why would you use the Form_Initialize event rather than the Form_Load event?
    C-Dog

  2. #2
    Guest
    Form_Initialize occurs when an application creates an instance of a Form, MDIForm, or class.

    Syntax

    Private Sub object_[n]Initialize( )[/b]

    The object placeholder represents an object expression that evaluates to an object in the Applies To list.

    Remarks

    You trigger the Initialize event when you:

    Use the CreateObject function to create an instance of a class. For example:

    • Set X = CreateObject("Project1.MyClass")
    • Refer to a property or event of an automatically created instance of a form or class in your code. For example:

    MyForm.Caption = "Example"

    Use this event to initialize any data used by the instance of the Form, MDIForm, or class. For a Form or MDIForm, the Initialize event occurs before the Load event.


    [/code]


    Form_Load occurs when a form is loaded. For a startup form, occurs when an application starts as the result of a Load statement or as the result of a reference to an unloaded form's properties or controls.

    Syntax

    Private Sub Form_Load( )
    Private Sub MDIForm_Load( )

    Remarks

    Typically, you use a Load event procedure to include initialization code for a formfor example, code that specifies default settings for controls, indicates contents to be loaded into ComboBox or ListBox controls, and initializes form-level variables.
    The Load event occurs after the Initialize event.
    When you reference a property of an unloaded form in code, the form is automatically loaded but isn't automatically made visible unless the MDIChild property is set to True. If an MDIForm object isn't loaded and an MDI child form is loaded, both the MDIForm and the child form are automatically loaded and both become visible. Other forms aren't shown until you either use the Show method or set the Visible property to True.
    The following code in an MDIForm Load event automatically loads an MDI child form (assuming Form1 has its MDIChild property set to True):

    Dim NewForm As New Form1

    NewForm.Caption = "New Form" ' Loads form by reference.

    Because all child forms become visible when loaded, the reference to the Caption property loads the form and makes it visible.

    [/code]
    Note When you create procedures for related events, such as Activate, GotFocus, Paint, and Resize, be sure that their actions don't conflict and that they don't cause recursive events.
    [/code]

  3. #3
    Guru Yonatan's Avatar
    Join Date
    Apr 1999
    Location
    Israel
    Posts
    892
    You probably don't need to care about the Initialize event.
    It is used when using a form as an object.
    Code:
    Dim MyForm As frmTemplate
    ' No event yet
    
    Set MyForm = New frmTemplate
    ' Initialize
    
    Call Load(MyForm)
    ' Load
    
    Call MyForm.Show
    DoEvents
    ' Resize, Activate, GotFocus, Paint (in that order)
    
    Call Unload(MyForm)
    ' QueryUnload, Unload (in that order)
    
    Set MyForm = Nothing
    ' Terminate
    Hope this helps!

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Jul 2000
    Location
    Boulder, Colorado
    Posts
    22

    Talking Thanks for your help!!

    I really appreciate your input. I'm still having trouble imagining a scenario where it would be better to put initialization code into the Initialize event rather than the Load event. I understand the order in which these events occur, and also what causes them to occur. Could someone please give me a practical example??!!
    C-Dog

  5. #5
    Guru Aaron Young's Avatar
    Join Date
    Jun 1999
    Location
    Red Wing, MN, USA
    Posts
    2,177
    One practicle use would be if you wanted to intercept WM_CREATE messages, normally you would hook the thread in a Module then launch the Form, but instead you can just hook the thread in the Forms Initialize Event. This example removes the Scrollbar from a Listbox by intercepting it's Creation Message and altering it's Style:

    In a Module:
    Code:
    Option Explicit
    
    Private Type CWPSTRUCT
            lParam As Long
            wParam As Long
            message As Long
            hwnd As Long
    End Type
    
    Private Type CREATESTRUCT
            lpCreateParams As Long
            hInstance As Long
            hMenu As Long
            hWndParent As Long
            cy As Long
            cx As Long
            y As Long
            x As Long
            style As Long
            lpszName As Long 'string
            lpszClass As Long 'string
            ExStyle As Long
    End Type
    
    Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
    
    Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
    Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
    Private Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hwnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    
    Private Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long
    
    Private Declare Function SetWindowsHookEx Lib "user32" Alias "SetWindowsHookExA" (ByVal idHook As Long, ByVal lpfn As Long, ByVal hmod As Long, ByVal dwThreadId As Long) As Long
    Private Declare Function UnhookWindowsHookEx Lib "user32" (ByVal hHook As Long) As Long
    Private Declare Function CallNextHookEx Lib "user32" (ByVal hHook As Long, ByVal ncode As Long, ByVal wParam As Long, lParam As Any) As Long
    
    Private Const WH_CALLWNDPROC = 4
    
    Private Const GWL_WNDPROC = (-4)
    Private Const GWL_STYLE = (-16)
    
    Private Const WM_CREATE = &H1
    Private Const WS_VSCROLL = &H200000
    
    Private lHook As Long
    Private lSubList As Long
    
    Public Sub HookThread()
        lHook = SetWindowsHookEx(WH_CALLWNDPROC, AddressOf HookApp, App.hInstance, App.ThreadID)
    End Sub
    
    Public Sub UnHookThread()
        Call UnhookWindowsHookEx(lHook)
    End Sub
    
    Private Function HookApp(ByVal lHookID As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
        Dim tCWP As CWPSTRUCT
        Dim sClass As String
    
        'Monitor this application thread for Window Creation Messages
        Call CopyMemory(tCWP, ByVal lParam, Len(tCWP))
    
        If tCWP.message = WM_CREATE Then
            'Looking for a Listbox class...
            sClass = Space(128)
            Call GetClassName(tCWP.hwnd, ByVal sClass, 128)
            sClass = Left(sClass, InStr(sClass, Chr(0)) - 1)
            If InStr(LCase(sClass), "listbox") Then
                'When found, Subclass this Window before the Listbox is created
                lSubList = SetWindowLong(tCWP.hwnd, GWL_WNDPROC, AddressOf SubListCreate)
            End If
        End If
        HookApp = CallNextHookEx(lHook, lHookID, wParam, ByVal lParam)
    End Function
    
    Private Function SubListCreate(ByVal hwnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
        Dim tCreate As CREATESTRUCT
        
        'Capture the Create Message for the Listbox and Remove the Vertical Scrollbar from it's Style
        If Msg = WM_CREATE Then
            Call CopyMemory(tCreate, ByVal lParam, Len(tCreate))
            tCreate.style = tCreate.style Xor WS_VSCROLL
            Call CopyMemory(ByVal lParam, tCreate, Len(tCreate))
            Call SetWindowLong(hwnd, GWL_STYLE, tCreate.style)
            'Remove the SubClassing for the Listbox
            Call SetWindowLong(hwnd, GWL_WNDPROC, lSubList)
        End If
        SubListCreate = CallWindowProc(lSubList, hwnd, Msg, wParam, lParam)
    End Function
    In the Form:
    Code:
    Private Sub Form_Initialize()
        'Hoop the Application Thread before the Controls get a chance to be created
        HookThread
    End Sub
    
    Private Sub Form_Load()
        Dim lIndex As Long
        
        For lIndex = 1 To 100
            List1.AddItem "Item " & lIndex
        Next
    End Sub
    
    Private Sub Form_Terminate()
        'Remove the Hook
        UnHookThread
    End Sub

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