如何检测用户控件是否在 IDE、调试模式或已发布的 EXE 中运行?

How can I detect whether a user control is running in the IDE, in debug mode, or in the released EXE?(如何检测用户控件是否在 IDE、调试模式或已发布的 EXE 中运行?)
本文介绍了如何检测用户控件是否在 IDE、调试模式或已发布的 EXE 中运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个用户控件.它的目的是向用户显示类的状态.显然,这无关紧要,并且当控件在 IDE 中运行时会减慢速度,就像将它添加到表单时一样.

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?

附言我正在使用 C# 和 VS 2008

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 中运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)