在设计自定义控件时,经常需要在构造函数或者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