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 :)
Empi - 26 Jul 2007 07:49 GMT
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
> Hi
>
[quoted text clipped - 17 lines]
> I'm using Visual-Studio 2005 VB.NET.
> Thanks :)
Peter Duniho - 26 Jul 2007 08:21 GMT
> 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.designerpropertie
s.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
count0 - 26 Jul 2007 11:48 GMT
> 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,
Mythran - 26 Jul 2007 16:29 GMT
>> Hi
>>
[quoted text clipped - 9 lines]
>
> 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
deneb - 27 Jul 2007 00:39 GMT
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.