本文转载:http://blog.csdn.net/sabty/article/details/5325260
以前也曾遇到这样的问题,不过影响不大也没有去详细了解。今天又重新遇到此问题,实在太不便。经查证这是 Visual Studio 2005 的 Bug。微软对此的 Bug 描述:http://support.microsoft.com/?scid=kb;zh-cn;839202&x=10&y=15
//DesignMode存在BUG,在构造函数里面DesignMode永远都是false,
//建议:不要把代码写在此处。否则每次"重新"解决方案时候都会执行。
建议把初始化代码代码写在FormLoad事件里面或者重载 protected override void OnCreateControl()。
解决方法:
在你的 Form 控件中重写 DesignMode 属性,代码如下:
- /// <summary>
- /// 标题:获取一个值,用以指示 System.ComponentModel.Component 当前是否处于设计模式。
- /// 描述:DesignMode 在 Visual Studio 2005 产品中存在 Bug ,使用下面的方式可以解决这个问题。
- /// 详细信息地址:http://support.microsoft.com/?scid=kb;zh-cn;839202&x=10&y=15
- /// </summary>
- protected new bool DesignMode
- {
- get
- {
- bool returnFlag = false;
- #if DEBUG
- if (System.ComponentModel.LicenseManager.UsageMode == System.ComponentModel.LicenseUsageMode.Designtime)
- {
- returnFlag = true;
- }
- else if (System.Diagnostics.Process.GetCurrentProcess().ProcessName.ToUpper().Equals("DEVENV"))
- {
- returnFlag = true;
- }
- #endif
- return returnFlag;
- }
- }
public partial class UserControl3 : UserControl
{
public UserControl3()
{
InitializeComponent();
//DesignMode存在BUG,在构造函数里面DesignMode永远都是false,
//建议:不要把代码写在此处。否则每次"重新"解决方案时候都会执行。
//if (this.DesignMode==false)
// MessageBox.Show(Application.StartupPath);
}
private bool _IsViewMode = false;
public bool IsViewMode
{
get
{
if (this.DesignMode == false)
MessageBox.Show(Application.StartupPath);
return _IsViewMode;
}
}
private void UserControl3_Load(object sender, EventArgs e)
{
if (this.DesignMode == false)
MessageBox.Show(Application.StartupPath);
}
{
public UserControl3()
{
InitializeComponent();
//DesignMode存在BUG,在构造函数里面DesignMode永远都是false,
//建议:不要把代码写在此处。否则每次"重新"解决方案时候都会执行。
//if (this.DesignMode==false)
// MessageBox.Show(Application.StartupPath);
}
private bool _IsViewMode = false;
public bool IsViewMode
{
get
{
if (this.DesignMode == false)
MessageBox.Show(Application.StartupPath);
return _IsViewMode;
}
}
private void UserControl3_Load(object sender, EventArgs e)
{
if (this.DesignMode == false)
MessageBox.Show(Application.StartupPath);
}