Results 1 to 5 of 5

Thread: Question of curiosity - Form_Load vs Form_Activate

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Apr 2017
    Posts
    554

    Question of curiosity - Form_Load vs Form_Activate

    In the few years I've been programming in VB I never once used the Form_Activate event; it never entered my mind. If I initialize all my variables and set all initial conditions in Form_Load what reason would I use Form_Activate for.

  2. #2
    Lively Member
    Join Date
    May 2017
    Posts
    81

    Re: Question of curiosity - Form_Load vs Form_Activate

    Hi. Does this thread answer your question? http://www.vbforums.com/showthread.p...Form-Form-Load

  3. #3
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    9,937

    Re: Question of curiosity - Form_Load vs Form_Activate

    Hi Code Dummy,

    I frequently use both. And I sometimes use them the same way, and have a mbAlreadyActivated flag at the top of the module. First, Form_Activate fires each time your form gets the focus within your application. (That "within your application" is important. It does not fire when switching applications.)

    So, if your program has two visible forms, and the user can switch between them, the Form_Activate may fire often whereas the Form_Load will only fire once.

    So, why do I use Form_Activate sometimes rather than Form_Load? Well, for certain things, the controls aren't fully loaded during Form_Load. Therefore, I need to wait until Form_Activate to do these things. For example, I've got code that will dynamically move controls around on the SSTab control's tabs. However, that code will crash if it's in Form_Load, so I have to wait for Form_Activate, but I only want to run it once. Hence, my mbAlreadyActivated flag.

    While we're at it, why don't we also talk about Form_Initialize (and Form_Terminate, which is its inverse). The Form_Load and Form_Activate events have to do with the actual window (with the associated hWnd) that the user sees. However, the Form_Initialize has to do with the COM/Code section of the form. The Code section of a form is extremely similar to the code in a Class module. It just also has that window (with hWnd) section as well. So, with this understanding, Form_Initialize is basically the same thing as Class1_Initialize. It fires when the COM/Code section is instantiated.

    Hope That Helps,
    Elroy
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  4. #4
    Fanatic Member
    Join Date
    Feb 2019
    Posts
    706

    Re: Question of curiosity - Form_Load vs Form_Activate

    See MSDN help here. One example I could think of if you have a toolbar/toolbox window, and the contents should switch or enabled/disabled to reflect the type of the form the user is working on, then you would make the visual changes in Activate event. For most apps, it's not needed.

  5. #5
    Frenzied Member
    Join Date
    Dec 2008
    Location
    Melbourne Australia
    Posts
    1,487

    Re: Question of curiosity - Form_Load vs Form_Activate

    There are some things that don't work in Form_Load but they will in Form_Activate (As Elroy mentions).
    Thus I put a lot of stuff in Form_Activate instead of Form_Load
    Form_Activate can fire more than once in the life of the form, so you need a precaution to handle that (ensure the code only executes once)
    Code:
    Option Explicit
    Private bInitialSetupCompleted As Boolean
    
    Private Sub Form_Activate()
      'Test to see if this is the first Activation, following Form_Load
        If bInitialSetupCompleted = False Then
              bInitialSetupCompleted = True '<== So the following code runs only once
              some code
              some code
        End If
    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