How can I make a code in an event not execute in design time

  • Thread starter Thread starter Lucas Sain
  • Start date Start date
L

Lucas Sain

Hi,
How can I make a code in an event not execute in design time. I have a
Baseform that has X code in the event "VisibleChanged". All my forms
inherit from this form. The code in this event always gets executed in
design time, how do I avoid this??

Regards
Lucas
 
Lucas,

The Control class derives from Component. On the Component class, there
is a property, DesignMode. If this property returns true, then your code is
running in a designer. You can place a check around the firing of your
event to see if your control is hosted in a designer, and not call it if
that is the case.

Hope this helps.
 
Thanks
It worked great.

regards
Lucas

Nicholas Paldino said:
Lucas,

The Control class derives from Component. On the Component class, there
is a property, DesignMode. If this property returns true, then your code is
running in a designer. You can place a check around the firing of your
event to see if your control is hosted in a designer, and not call it if
that is the case.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Lucas Sain said:
Hi,
How can I make a code in an event not execute in design time. I have a
Baseform that has X code in the event "VisibleChanged". All my forms
inherit from this form. The code in this event always gets executed in
design time, how do I avoid this??

Regards
Lucas
 
Lucas said:
Hi,
How can I make a code in an event not execute in design time. I have a
Baseform that has X code in the event "VisibleChanged". All my forms
inherit from this form. The code in this event always gets executed in
design time, how do I avoid this??

Regards
Lucas

Hold on, DesignMode isn't reliable.

If you have a component within a component, the DesignMode property on
the embedded component is **always** wrong (ie. false within the designer).

Instead, use the following, it'll save a lot of pain:

// If this returns NULL, we're in DesignMode.
if ( System.Reflection.Assembly.GetEntryAssembly() == null )
{
// We're in DesignMode.
}

Get into the habit of using this, and NOT the DesignMode property.

An alternative is to check the name of the entry assembly. If you're in
VS.NET, this will return [devenv.exe]. I prefer the above.

If you think this is suspect, do a quick check on Google, there are some
postings out there on the topic.

Sean
 
Hold on, DesignMode isn't reliable.

I can vouch for DesignMode not being accurate, but I thought it was based on
me trying to use it in the Control's constructor.

The docs also state the following:
'The design mode indicator is stored in the ISite; therefore, if the
Component does not have an ISite associated with it, this property is always
false.'

-Eric
 
Sean,

Thanks will use your sugestion instead.

Regards
Lucas
Sean J Donovan said:
Lucas said:
Hi,
How can I make a code in an event not execute in design time. I have a
Baseform that has X code in the event "VisibleChanged". All my forms
inherit from this form. The code in this event always gets executed in
design time, how do I avoid this??

Regards
Lucas

Hold on, DesignMode isn't reliable.

If you have a component within a component, the DesignMode property on
the embedded component is **always** wrong (ie. false within the designer).

Instead, use the following, it'll save a lot of pain:

// If this returns NULL, we're in DesignMode.
if ( System.Reflection.Assembly.GetEntryAssembly() == null )
{
// We're in DesignMode.
}

Get into the habit of using this, and NOT the DesignMode property.

An alternative is to check the name of the entry assembly. If you're in
VS.NET, this will return [devenv.exe]. I prefer the above.

If you think this is suspect, do a quick check on Google, there are some
postings out there on the topic.

Sean
 
Back
Top