Luke Belbina
Luke Belbina

Reputation: 5859

.NET Design View Not Running Windows Forms OnLoad

I have a windows application which works when the application is running, however in design view, the code in the OnLoad event crashes due to a host of reasons. Is there a way to do something like this:

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

Upvotes: 1

Views: 880

Answers (3)

Christopher Currens
Christopher Currens

Reputation: 30695

There is the DesignMode property inheritied from Component.

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

Though there is a better way to do it than that, since if I recall correctly, sometimes there can be issues with the DesignMode property. I think I have some code somewhere, let me find it.

EDIT: Well, I can't find what I was thinking of, but this answer discusses some of the downsides of DesignMode you should keep in mind, as well as a workaround for a specific issue. However, the issue doesn't affect what you want to do here, it doesn't look like, but it's good to be aware of it anyway.

Upvotes: 3

Reed Copsey
Reed Copsey

Reputation: 564333

You can check the DesignMode property of the form.

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

Upvotes: 0

competent_tech
competent_tech

Reputation: 44921

Yes, use this:

if (!this.DesignMode)

Upvotes: 0

Related Questions