C # usercontrol judge designMode

Source: Internet
Author: User
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
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.