Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I detect whether a user control is running in the IDE, in debug mode, or in the released EXE?

I have a user control that I'm building. It's purpose is to display the status of a class to the user. Obviously, this does not matter, and will slow things down when the control runs in the IDE, as it does as soon as you add it to a form.

One way to work around this would be to have the control created and added to the controls collection of the form at run-time. But this seems less than perfect.

Is there a way to set a flag in the control so that it can skip certain sections of code based on how it is running?

p.s. I'm using C# and VS 2008

like image 874
RichieACC Avatar asked Dec 03 '08 10:12

RichieACC


2 Answers

public static bool IsInRuntimeMode( IComponent component ) {
    bool ret = IsInDesignMode( component );
    return !ret;
}

public static bool IsInDesignMode( IComponent component ) {
    bool ret = false;
    if ( null != component ) {
        ISite site = component.Site;
        if ( null != site ) {
            ret = site.DesignMode;
        }
        else if ( component is System.Windows.Forms.Control ) {
            IComponent parent = ( (System.Windows.Forms.Control)component ).Parent;
            ret = IsInDesignMode( parent );
        }
    }

    return ret;
}
like image 161
TcKs Avatar answered Oct 25 '22 16:10

TcKs


I found this answer on another post and it seems to be easier and working better for my situation.

Detecting design mode from a Control's constructor

using System.ComponentModel;

if (LicenseManager.UsageMode == LicenseUsageMode.Runtime)
{
}
like image 26
Ben Gripka Avatar answered Oct 25 '22 14:10

Ben Gripka