How to have code in the constructor that will NOT be executed at design time by Visual Studio?
Asked Answered
T

6

18

I have a method call in the constructor of my user control that does something that won't work at design time (connecting to a database), and Visual Studio just bailed out when I tried to add that control to the GUI designer.
Sure, I can factor out that code to a separate method, but I don't like the idea that every time I use that object I need to remember to execute a certain method which is essential to that object's function (that's what the constructor is for!).

Is there something like a preprocessor symbol that I can mark my code with so that Visual Studio won't try to execute that code at design time?

Twana answered 21/11, 2009 at 6:15 Comment(2)
Is that a good design? IMO, one should not do too much (connecting to database, in your case) in the constructor. I would love to see what experts have to say.Copenhagen
Please add a tag to indicate what technology you are talking about here. Is it windows forms? WPF? AspNet WebForms? Something else?Rime
J
28

As others have stated, you can use the DesignMode property of the Component class. However, you will not be able to do this in the constructor of your control. The DesignMode property is always false in the constructor and methods called by the constructor. To get around this, re-factor your code to connect to the database in the OnLoad() callback. The DesignMode property is valid at that point. See here for the reasoning (look for the DesignMode section of the article).

I just ran across this blog entry that describes how to use the System.ComponentModel.LicenseManager.UsageMode property for doing the same thing. The blog describes an additional shortcoming of the DesignMode property when dealing with nested controls. Apparently, the UsageMode property doesn't have the same shortcomings and is available for use in the constructor. I cannot personally vouch for it, but might be worthwhile looking into.

Job answered 21/11, 2009 at 6:27 Comment(3)
The blog post you linked works perfectly for me! (at least so far)Twana
That's good to know. I may leverage that technique the next time I need a custom control.Job
Blog is now dead but basically it's just: if (System.ComponentModel.LicenseManager.UsageMode == System.ComponentModel.LicenseUsageMode.Designtime)Maurizio
S
16

In Windows Forms?

if (!DesignMode)
{
    // code that shouldn't be executed at design time
}

As others have mentioned, this won't work in the constructor. It's often used in the Form.Load event.

Squiggle answered 21/11, 2009 at 6:20 Comment(6)
Brilliant, exactly the same time X-)Jiminez
Quick and right to the point! I was expecting some preprocessor symbol since that won't affect the program after compilation, but the impact of this small check is of course minimal ;)Twana
please be careful ! DesingMode property has some issues that should be considered.You can find an article here: weblogs.asp.net/fmarguerie/archive/2005/03/23/395658.aspxFoliate
Yes I realize that (by the hard way) soon enough. The answer I marked as answer works though.Twana
Sorry about that; I could've sworn I've used it in constructors.Squiggle
This approach worked perfectly for my workflow. I used if (!IsInDesignMode) {...} for WPF.Gastongastralgia
J
1

Have a look at this

Component.DesignMode Property

Jiminez answered 21/11, 2009 at 6:20 Comment(0)
G
0

I liked Michael Petrotta's approach for Windows Forms. If anyone wants to apply the same technique to WPF, simply use IsInDesignMode.

Example:

public SomeViewModel()
{
    if (!IsInDesignMode)
    {
        DoWork();
    }
}
Gastongastralgia answered 9/12, 2014 at 8:0 Comment(0)
D
0
public SomeViewModel()
{
    if (!IsInDesignMode)
    {
        DoWork();
    }
}

This code above if you are working on the actual UI that you are trying to work on. In a situation that you have something like this on a control, when you switch back to the designer for that control it's ok and no design time error. Now if you added that control that contains the code above to some other Form or another control via dragging it from the toolbox, it will show some design time errors.

Dubose answered 29/3, 2017 at 6:50 Comment(0)
P
0

This is the only code that worked for me, using WPF UserControl on a Windows Form.

private bool? inDesignMode;
public bool IsDesignMode
{
  get
  {
    if (inDesignMode == null)
    {
      var prop = System.ComponentModel.DesignerProperties.IsInDesignModeProperty;

      inDesignMode = (bool)System.ComponentModel.DependencyPropertyDescriptor
          .FromProperty(prop, typeof(FrameworkElement))
          .Metadata.DefaultValue;

      if (!inDesignMode.GetValueOrDefault(false) && System.Diagnostics.Process.GetCurrentProcess().ProcessName.StartsWith("devenv", StringComparison.Ordinal))
        inDesignMode = true;
    }

    return inDesignMode.GetValueOrDefault(false);
  }
}
Poundfoolish answered 5/9, 2022 at 12:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.