views:

1128

answers:

2

Whenever we load a VB project it will call initialize method of a User Control ( if there is any in the project). My problem is that is that I have some code in UserControl.initialize that will try to create instances of other COM objects. And on my build machine those controls are not registered. One option is to move the code to some other method other than initialize but I want to know if there is a better solution? Somewhere I found that we may have a check to see if the calling application is VB editor then skip the initialization code...

+1  A: 

This only happens if your project was saved with the form designer open: this means that at startup the form is displayed (maybe in the background) and consequently, all controls on it need to be initialized. Hence, 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.

Konrad Rudolph
Yup you are right. But my problem is that I can't make sure that each of my developer checks in the project file with the designer closed :(
Paragon
+3  A: 

You can use:

If Not Me.DesignMode Then
  ...
End If

An other solution we used was a little function which 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

Don't know if it is syntactically well formed, but the idea should be clear: Only in the IDE the debug statement is called.

H-Man2
Thanks H-Man2 :). The second solution is great. As for the first solution Me.DesignMode is not an option in VB6, perhaps it was introduced in VB.NET
Paragon
The first solution should be If Not Ambient.UserMode for VB6. For example http://msdn.microsoft.com/en-us/library/aa241637(VS.60).aspx
MarkJ
Note that the two solutions have slightly different behaviour. The first solution always switches off the code when the control is in design-mode (only makes a difference if you build your control into an OCX). The second solution always switches the code off when running from the IDE whether in design mode *or in run mode*. Might stop your control working properly when running from the IDE to test run-time behaviour.
MarkJ