Some of my forms show errors when i load them in the designer. This is because in their constructors they use system settings loaded from a configuration file. When the form is loaded in the designer it uses some random path and unsurprisingly the config file isn't there.
eg. The configuration file C:\Documents and Settings\Rory\Local Settings\Application Data\Microsoft\VisualStudio\8.0\ProjectAssemblies\it3dtcgg01\PrioryShared.dll.config could not be found.
Is there some way I can deal with this so that the form displays correctly in the designer? eg:
if (!inDesignerMode)
{
loadSettingsFromConfigFile();
}
UPDATE: ok, I'm still getting this error. The composition is like this
MyForm.cs
- MyCustomControl.cs
In MyCustomControl's constructor I've put
if (!this.DesignMode)
{
// Get settings from config file <- this is where the error occurs
}
but it's on that line that I still get the error in the designer. What gives?
UPDATE: Worth noting this link that describes how to debug design-time controls.
UPDATE: Control.DesignMode isn't set to true when called within the constructor (MSDN) of the object! So that sort of code should go in the onLoad. Alternatively you can use an approach like this
-
Page.DesignMode
This can be used in all pages and user controls.
Marc Gravell : Page? The tags suggest this is winformsJeff Yates : I believe Page is a WPF or Silverlight concept.: You misunderstood, when I say "Page" i mean System.Web.UI.Page, which derives from Control. -
How about simply putting that logic into
OnLoad
instead, and checkDesignMode
?protected override void OnLoad(System.EventArgs e) { base.OnLoad(e); if (!this.DesignMode) { /* do stuff */ } }
Rory : The line of code inside (!this.DesignMode) is still being executed at design mode - how can that be?Rory : See update - DesignMode isn't correct when called in the constructor -
You should wrap the items in a check on
this.DesignMode
, which is set totrue
when in the designer, or move them toOnLoad
or some other place that doesn't get called during designer use. -
Alternatively, you can do something like this:
public bool IsDesignMode { get { return (System.Diagnostics.Process.GetCurrentProcess().ProcessName == "devenv"); } }
-
This is what I do when I have to do some special handling if the control is currently used by the designer.
public MyControl() { InitializeComponent(); if (LicenseManager.UsageMode != LicenseUsageMode.Designtime) { // Put stuff here that should not be run while in the designer } }
-
I've always found it to be a mistake for a control to use a configuration file. Instead, the control should have settable properties, and if the form using it wants to set those properties from configuration, then it's welcome to do so.
0 comments:
Post a Comment