views:

356

answers:

1

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

+7  A: 
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;
}
TcKs
Awesome! Thanks. That's a lot easier than I thougt it would be!
RichieACC
Any thoughts on why Site property is always null on my Page object?
TGnat
Because webforms designer does not support this scenario. I don't know why :(. This solution works in component ( non UI components) designer and winforms designer. It should works in non-web 3rd party designers too.
TcKs
Thanks... You got my vote anyway for a quality answer...
TGnat