.NET设计视图未运行Windows窗体OnLoad

时间:2011-11-30 18:35:27

标签: c# .net windows winforms

我有一个Windows应用程序,它在应用程序运行时有效,但在设计视图中,OnLoad事件中的代码由于许多原因而崩溃。有没有办法做这样的事情:

private void WindowsForm_OnLoad(object sender, EventArgs e)
    {
        if (IsDesignView())
        {
               // some code that breaks in design view but works normally
        }

3 个答案:

答案 0 :(得分:3)

从Component继承了DesignMode属性。

if(!this.DesignMode) {
    // Your stuff...
}

虽然有更好的方法可以做到这一点,因为如果我没记错的话,有时可能会出现DesignMode属性的问题。我想我在某个地方有一些代码,让我找到它。

编辑:好吧,我找不到我的想法,但是this answer讨论了你应该记住的DesignMode的一些缺点,以及特定问题的解决方法。但是,这个问题不会影响你想要做的事情,看起来不像,但无论如何都要注意它。

答案 1 :(得分:0)

是的,请使用:

if (!this.DesignMode)

答案 2 :(得分:0)

您可以查看表单的DesignMode属性。

if (!this.DesignMode)
{
    // Include code that breaks the designer here...
}
相关问题