Original article address:C #UsercontrolJudge designMode   
Author:Mousekitty  
 
     When developing usercontrol in. net, we often need to know whether it is the design mode or runtime mode.
 
 
   in design mode ,. net does not run your system. It only initializes the current control and all its parent classes, including its constructor, onload (), oninit (), and other methods. In onload or oninit, if you write  Code  that can be successfully executed only in runtime mode, when you view this window in form design mode, you may get an error "the object is empty or not initialized", so that you cannot create an instance of this control. I also encountered a more severe situation, that is, when the control I wrote was in runtime 
 mode, everything went fine, but in design mode, once the file was opened, vs2005 was immediately restarted, I should admire myself for such a powerful control. Let's get down to the truth, so how can we deal with this situation? A simple method is to write try and catch and eat exceptions. Another way is to judge whether it is design 
 mode or runtime mode, perform different processing. The Code is as follows:  
 
// Running environment: windows7 + vs2005 + framework2.0sp2
 
 
 
// This. designMode = true is invalid.
 
 
  If (this. getservice (typeof (idesignerhost ))! = NULL | system. componentmodel. licensemanager. usagemode = system. componentmodel. licenseusagemode. designtime) 
{< br>  // design mode 
}< br> else 
{< br>   // runtime mode 
}