VB6.0: initialize the User Control method that is called when the VB project is loaded

Whenever we load a VB project, it raises the Initialize event of the user control (if there is one in the project). My problem is that I have code in UserControl_Initialize that will try to instantiate other COM objects. On my build machine, these controls are not registered. One option is to move the code to a different method than Initialize , but I want to know if there is a better solution? Somewhere I found that we can have a check to check if the calling application is a VB editor and then skip the initialization code ...

+4
source share
2 answers

You can use:

 If Not Me.DesignMode Then ... End If 

Another solution we used was a small function that can be used globally:

 Public Function IsRuntime() as Boolean On Error Goto NotRuntime Debug.Print(1 / 0) IsRuntime = True Exit Function NotRuntime: IsRuntime = False End If 

I don’t know if it is syntactically formed, but the idea should be clear: Only in the IDE is the debug command called.

+5
source

This happens only if your project was saved when the form designer was opened: this means that when the form starts up, it appears (possibly in the background), and therefore all controls on it must be initialized. Therefore, your user control initializer is called if this control is used on the form.

To prevent this, simply save the project with the form designer closed.

+1
source

All Articles