How can I detect whether a user control is running in the IDE, in debug mode, or in the released 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 中运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何检测用户控件是否在 IDE、调试模式或已发布的 EXE 中运行?
基础教程推荐
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01
- 将 XML 转换为通用列表 2022-01-01
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30
- rabbitmq 的 REST API 2022-01-01
- MS Visual Studio .NET 的替代品 2022-01-01
- SSE 浮点算术是否可重现? 2022-01-01
- c# Math.Sqrt 实现 2022-01-01
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01
- 如何激活MC67中的红灯 2022-01-01