本文转载: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 属性,代码如下:

  1. /// <summary>   
  2. /// 标题:获取一个值,用以指示 System.ComponentModel.Component 当前是否处于设计模式。   
  3. /// 描述:DesignMode 在 Visual Studio 2005 产品中存在 Bug ,使用下面的方式可以解决这个问题。   
  4. ///        详细信息地址:http://support.microsoft.com/?scid=kb;zh-cn;839202&x=10&y=15   
  5. /// </summary>   
  6. protected new bool DesignMode  
  7. {  
  8.     get  
  9.     {  
  10.         bool returnFlag = false;  
  11. #if DEBUG   
  12.         if (System.ComponentModel.LicenseManager.UsageMode == System.ComponentModel.LicenseUsageMode.Designtime)  
  13.         {  
  14.             returnFlag = true;  
  15.         }  
  16.         else if (System.Diagnostics.Process.GetCurrentProcess().ProcessName.ToUpper().Equals("DEVENV"))  
  17.         {  
  18.             returnFlag = true;  
  19.         }  
  20. #endif   
  21.         return returnFlag;  
  22.     }  
  23. }  

 

  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);
        }
View Code

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-01-01
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-06-09
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案