• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 215
  • Last Modified:

Form_Load

We have a VB application that runs all the time in the background (effectively as a service). However, it does contain a form that is only used in another mode (the program can run in a manual mode). It therefore contains a Form_Load function. Under what circumstances can Form_Load be called? I know, for example, that Form_Unload gets called if a user logs off from Windows.

Does anyone know all events that might cause Form_Load to be called?

0
simonslr
Asked:
simonslr
  • 2
  • 2
  • 2
  • +5
1 Solution
 
Ryan ChongCommented:
Form_Load event only fired once when a form is loading. What i would suggest is make the codes in Form_Load event as a Sub then call it like this:

Private Sub Form_Load()
    Call mySub
End Sub

Private Sub mySub()
//Do the stuff here
End Sub

'You can put the Sub in a Module by change 'Private' to 'Public so that it can be called by another forms
0
 
HeN_da_MaNCommented:
You could use Form_Activate, I don't know what U want to do.
Form_Load gets called whenever you call a function in the codemodule and your form is not active.
You could also call Form.Show to call Form_Load (this works only one time until you Unload the form again)
Maybe I am able to help you if you give more info.

Hen
0
 
Ryan ChongCommented:
To be more clear on my comment, create a Sub so that we can remember the specific name of the function to call (rather than Form_Load). Of course you still can Call Form_Load within your form, like this:

Private Sub Command1_Click()
    Form_Load
End Sub

Private Sub Form_Load()
    MsgBox "Hello World"
End Sub
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
TimCotteeCommented:
Well there are basically three ways to call the form_load procedure code:

1) Load the form; Load frmMyForm
2) Call it explicitly (requires that the form is already loaded and is being called from a procedure within the form's scope); Call Form_Load()
3) Reference a public method or object in the form; this is a tricky one as if you have not yet explicitly loaded a form, any direct reference to an object or method of the form will cause it to be implicitly loaded. For example if you have a textbox on the form called text1 then any reference in any other part of the project such as frmMyForm.Text1.Text = "Hello" will actually cause the form to be loaded (if it isn't already) and the Form_Load event code will be executed.

From what you say (and what you don't) my assumption is that because you have seen the form_unload code running when the application shuts down as a result of windows shutting down then you wonder how the form was ever opened. My suggestion is that this is caused by 3) above and that somewhere in your code you are referencing an object or method in that form which is causing it to be implicitly loaded but never shown.
0
 
BahnassCommented:
Bravo Super Tim !!!!
0
 
pierrecampeCommented:
Hi Tim just a remark
*********************
Reference a public method or object in the form; this is a tricky one as if you have not yet explicitly
loaded a form, any direct reference to an object or method of the form will cause it to be implicitly loaded
*********************
calling a public method in a form that is not loaded will NOT load the form
but the form object will be initialized
and thus its initialize event will be called
and on ending the program its terminate event will be called
but if you reference any of the VISIBLE propertys of the form it will be loaded
say you set its caption or a control in it, then it will be loaded
0
 
ngomteCommented:
Form Load is called whenever a refereence is made to a object to this form, other then the property procedure.
0
 
amebaCommented:
pierrecampe> if you reference any of the VISIBLE propertys of the form it will be loaded
or maybe we can say "if you reference any of the *built-in* properties"

     MsgBox Form2.Tag    ' this will load Form2
     
     MsgBox Form2.uhoh   ' this will not load Form2
0
 
simonslrAuthor Commented:
I don't think I've made my question very clear. I know that the Form_Load function is being called at start-up - we hide the form when running in non-manual mode ...

... but are there any circumstances that might cause Form_Load to be called again (for example in the same way that form_unload is called when a user logs off - in our case the program carries on running even when a user logs off).
0
 
simonslrAuthor Commented:
I don't think I've made my question very clear. I know that the Form_Load function is being called at start-up - we hide the form when running in non-manual mode ...

... but are there any circumstances that might cause Form_Load to be called again (for example in the same way that form_unload is called when a user logs off - in our case the program carries on running even when a user logs off).
0
 
TimCotteeCommented:
It should never be called again if it hasn't been unloaded. Unless called explicitly for some reason.
0
 
amebaCommented:
>are there any circumstances that might cause Form_Load
Without calling it in your code or referencing something on Form, no.
Are you sure there is an extra Form_Load executed?

>program carries on running even when a user logs off

Program can run without any form loaded.
Program can run without reexecuting Form_Load - e.g. form didn't unload for some reason:
- timer was active, some form code is stuck in a loop, circular references, noone called Unload in code, or someone called Unload, but that was cancelled in QueryUnload or Unload event....
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 2
  • 2
  • 2
  • +5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now