导航

窗体是否处于设计模式阶段

Posted on 2018-01-31 11:33  清浅ヾ  阅读(137)  评论(0编辑  收藏  举报

1.新建一个UserControl用户控件,代码如下:

    public partial class TestControl : UserControl
    {
        public TestControl()
        {
            InitializeComponent();

            if (!this.IsDesignMode())
            {
                this.textBox1.Text = "123非设计模式";
            }
            else
            {
                this.textBox1.Text = "456设计模式";
            }
        }

        public bool IsDesignMode()
        {
            bool returnFlag = false;

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

 

2.将用户控件拖入某个窗体,打开窗体设计界面如下:

 

 

3.开始运行后的窗体如下:

 

经过对比,上述代码可以解决处在设计模式下的窗体,指定或者禁用某一部分代码的运行