我怎么可以检测IDE用户控件是否在运行,在调试模式下,还是在发布EXE? [英] How can I detect whether a user control is running in the IDE, in debug mode, or in the released EXE?

查看:108
本文介绍了我怎么可以检测IDE用户控件是否在运行,在调试模式下,还是在发布EXE?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我建立一个用户控件。它的目的是为了展示一个类的状态给用户。显然,这并不重要,并会在IDE事情变慢的控制程序运行时,它只要你将它添加到一个表单呢。



的一种方式要解决这将是有控制创建并添加到在运行时的窗体控件集合。但这似乎并非完美。



有没有办法设置一个标志的控制,以便它可以跳过基于它是如何运行的代码的某些部分?



PS我使用C#和Visual Studio 2008


解决方案

 公共静态布尔IsInRuntimeMode(IComponent的组件) {
布尔RET = IsInDesignMode(组件);
返回沤!;
}

公共静态布尔IsInDesignMode(IComponent的组件){
布尔RET = FALSE;
如果(空=成分!){
的ISite网站= component.Site;
如果(空=网站!){
RET = site.DesignMode;
}
,否则如果(组件是System.Windows.Forms.Control的){
IComponent的父=((System.Windows.Forms.Control的)组件).Parent;
RET = IsInDesignMode(父);
}
}

返回RET;
}


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

解决方案

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;
}

这篇关于我怎么可以检测IDE用户控件是否在运行,在调试模式下,还是在发布EXE?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆