在设计自定义控件时,经常需要在构造函数或者Load事件中添加初始化代码,但是这些代码在进入窗体设计也会被执行,造成了设计窗口出现异常的情况。

使用下面的代码,可以让你判断出是否处于窗体设计模式,进而保证代码只会在最终用户使用时才会被执行。

 

    public static bool IsDesignMode()
{
bool returnFlag = false;

#if DEBUG
if (LicenseManager.UsageMode == LicenseUsageMode.Designtime)
{
returnFlag = true;
}
else if (Process.GetCurrentProcess().ProcessName == "devenv")
{
returnFlag = true;
}
#endif

return returnFlag;
}

来源:http://www.lukiya.com/Blogs/2010/05/20/Post-1012.html

相关文章:

  • 2022-12-23
  • 2021-07-03
  • 2021-10-14
  • 2021-08-24
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-11-24
猜你喜欢
  • 2021-09-01
  • 2022-12-23
  • 2021-11-18
  • 2022-12-23
  • 2021-11-15
  • 2021-07-30
  • 2022-12-23
相关资源
相似解决方案