Skip some code in Design Time.

  • Thread starter Thread starter deneb
  • Start date Start date
D

deneb

Hi

I want to prevent some code to run in design time.

I have a base form that have some code in its Load event.
And I create a form inherited from base form.

When I watch the form design in design time, codes that is in base
form's load event are excuted.
The problem is that because it's design time, it couldn't work well.
So it always return some errors.

As far as I know below code can prevent code to run in debug mode.

#if debug then
#end if

Is there something like that can prevent code in design time???

I'm using Visual-Studio 2005 VB.NET.
Thanks :)
 
Hi,

Why won't you define a special "flag" such as :

#Const DESIGNTIME = True



Public Class Form1

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

#If Not DESIGNTIME Then

MsgBox("Hello")

#End If

End Sub

End Class
 
I want to prevent some code to run in design time.

In the System.ComponentModel namespace, there are several Designer-related
code attribute classes defined. I haven't tried it myself, but one in
particular looks like it might be useful: DesignerProperties, which has an
IsInDesignMode property that can be used to check whether your component
is running within the Designer.

http://msdn2.microsoft.com/en-us/library/system.componentmodel.designerproperties.aspx

Note: this appears to be .NET 3.0 only. I didn't check the others...if
you need something that works in an earlier version, maybe one of the
other Designer-related properties would still do the job and yet is
available in an earlier .NET.

Pete
 
deneb said:
Hi

I want to prevent some code to run in design time.
[]
As far as I know below code can prevent code to run in debug mode.

#if debug then
#end if

Is there something like that can prevent code in design time???

Check Component.DesignMode property,
 
count0 said:
deneb said:
Hi

I want to prevent some code to run in design time.
[]
As far as I know below code can prevent code to run in debug mode.

#if debug then
#end if

Is there something like that can prevent code in design time???

Check Component.DesignMode property,

Microsoft.Net Framework v1.1.4322 and above (and may work for v1.0 as well):

if (this.Site != null && this.Site.DesignMode) {
... currently in design mode ...
} else {
... not currently in design mode ...
}

This works for us and we use it for web controls and web forms to prevent
code from executing during design-time (and for some forms, to make sure
some code ONLY runs during design time).

HTH,
Mythran
 
Thanks all guys.

Because I use .NET Framework 2.0, I can't use DesignerProperties in
System.ComponentModel of .NET Framework 3.0.
But, Component.DesignMode property does work well.

I really appreciate for your help.
 
Back
Top