UserControl Constructor Exception in Designer

UserControl Constructor Exception in Designer

Old forum URL: forums.lhotka.net/forums/t/1026.aspx


FireGarden posted on Friday, August 25, 2006

I have created a simple user control and have put a call to populate some business objects in the constructor. When I drag this control onto the designer an exception is thrown relating to the data portal.

See attached image. Is this a known issue? Is there a work around?

Rob Wheeldon

xal replied on Friday, August 25, 2006

Rob,

If you do this sort of thing in the constructor or a form load, you should do it inside something like this:

If Not Me.DesignMode Then
...
End If


--
Andrés

dean replied on Friday, August 25, 2006

Rob,

Andrés suggestion may not work.

According to this http://msdn.microsoft.com/msdnmag/issues/03/04/Design-TimeControls/  and my experience the DesignMode flag is not set to true until after the constructor is run. So you can use it in the load or other methods and it works but not the constructor.
 
Alternatively I started populating the business objects/binding sources from outside the user control ie from within the load method of the form that hosts the control.

Dean.

xal replied on Saturday, August 26, 2006

Rob, you might be right...
I usually use Factory methods that get the object and pass it to the constructor...
But, I believe the designer never calls your constructor. It calls the base class' constructor to make an instance of the object, and then uses the InitializeComponent() in order to get the designer to show your controls. That is why adding controls by hand in the constructor doesn't work with the designer.

Andrés

FireGarden replied on Saturday, August 26, 2006

For the record I changed the code in my user control as follows:

if (!this.DesignMode) PopulateControls();

This works perfectly.

Thank You.

 

dean replied on Sunday, August 27, 2006

Rob - was this in the constructor of your control?

Dean

FireGarden replied on Sunday, August 27, 2006

Yes but I think I missed something because its no longer working.

David replied on Monday, August 28, 2006

I ran into this issue a while back and was recommened to test for design mode with this:

Application.ExecutablePath.ToLower().IndexOf("devenv.exe") > -1

Ugly I know, but hidden in a function you never see and it works for me.

FireGarden replied on Monday, August 28, 2006

I changed the user control constructor as follows:

if (Application.ExecutablePath.ToLower().IndexOf("devenv.exe") > -1)
{
   PopulateControls();
}

and it now gives me the following exception when I try and load the main form:

The designer loader did not provide a root component but has not indicated why.

at System.ComponentModel.Design.DesignSurface.get_View()
at Microsoft.VisualStudio.Shell.Design.WindowPaneProviderService.CreateWindowPane(DesignSurface surface)
at Microsoft.VisualStudio.Design.Serialization.CodeDom.DeferrableWindowPaneProviderService.CreateWindowPane(DesignSurface surface)
at Microsoft.VisualStudio.Design.VSDesignSurface.Microsoft.VisualStudio.Designer.Interfaces.IVSMDDesigner.get_View()

David replied on Monday, August 28, 2006

Haven't you got that statement the wrong way around? It will currently only call PopulateControls if you ARE in design mode.

Copyright (c) Marimer LLC