How to know the design time and run time?

  • Thread starter Thread starter Guest
  • Start date Start date
Steven.Xu said:
How can I know whether it is design state or run state of C#.

You have to ask this question of something "designable", like an IComponent or
a Control (WinForms or ASP.NET Control). These types will all expose a
property named Site (of type System.ComponentModel.ISite). Note that Form
(in WinForms) and Page (in ASP.NET) also inherit from their respective Control
base classes, so they can be sited.

On ISite, there is a boolean property named 'DesignMode' that you can check.
It will be True when your component or control is in the design-mode.

For example, in ASP.NET within Page_Load you could write,

if ( ( Page.Site != null ) && ( Page.Site.DesignMode ) )
// Do design-time only operations here.
else
// Do run-time only operations here.

Before checking the DesignMode property, always test that the control is sited
(i.e., it's Site property is non-null). When null, that indicates the control is also
at run-time (no Site means no possibility of it being design-time).


Derek Harmon
 
Are you talking about Windows Forms? In this situation you can check the DesignMode property of the form or control. Be aware, however, that this value is not set in the constructor. If you want to do initialization checking of DesignMode then implement ISupportInitialize

Regards

Richard Blewett - DevelopMentor
http://www.dotnetconsult.co.uk/weblog
http://www.dotnetconsult.co.uk

How can I know whether it is design state or run state of C#.
 
I don't now where you need it for, but if something else should happen while
debugging than in the final release of your application, use compiler hints,
like this:

#if DEBUG
// Some code only to execute while debugging!
#endif

For this to work, activate your Debug configuration for the solution.

Gr,
Cybenny

Hello Steven.Xu,
 
Back
Top