affan affan - 1 year ago 114
C# Question

`DesignMode` in subcontrols is not set correctly?

I have a compound control contains nested controls. The problem i am facing is that control read properties from a global setting class which is static and intern read from setting file. To stop individual control from accessing configuration in design mode i added check in each control.

//Initialize properties e.g. prop = AppConfig.GetProperty("prop1");

The problem is that individual control work fine when open in VS. But when i open top control containing nested control i get error by VS designer. The error is that in a nested control
for some reason.

I also created a test app and created a simple control within another control to test if there is a problem with VS but it seem to work correctly for any depth of controls.

I dont even know how to debug this. For now i comment out the property initializing code and build it and then open designer and there uncomment it and build it again to run it.

Did anyone came across this problem or is there any way to fix it.

Answer Source

The problem you're facing is that the DesignMode is not set while in Constructor (at any level).

Move your initialization methods to Load event, to avoid this problem.

Also, you could add additional comparison with:

 protected bool IsInDesignMode
        return DesignMode || LicenseManager.UsageMode == LicenseUsageMode.Designtime;

This would definitely provide an acurate way to determine the design mode because sometimes VisualStudio returns DesignMode = false while you're placing user controls on the form, or refreshing the form display itself in design.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download