问题描述
我正在构建一个用户控件.它的目的是向用户显示类的状态.显然,这无关紧要,并且当控件在 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 中运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!