hi!
i have a big problem with the designer in vs2003/c#. i searched the internet
but can't find any solution to the problem described below...
i would like to create a usercontrol with some content in it. e.g. a
titlebar and a stateicon and so on. there is also a panel (i'll name it
contentPanel) in the middle of the usercontrol (dockstyle.fill) where the
content should be added at design time. For this panel a created a public
property in the usercontrol so that you can access them from outside.
in some posts i've readed that's not possible to add other controls at
designtime to this contentPanel - so my idea was to write a custom designer
for the usercontrol which create a new panel on the rootform and than adding
it to the controls-list of the contentPanel.
until now - everything is nice :-)
the code and the usercontrol works as expected. i can add controls at
designtime to my contentpanel which lies inside the usercontrol.
but - and thats the problem - there's a little error-message in my job-list:
"Die Codegenerierung f?r die Eigenschaft 'Controls' ist fehlgeschlagen.
Fehler: 'Der Objektverweis wurde nicht auf eine Objektinstanz festgelegt.'"
in english: "code-generation for the property 'Controls' was failed: the
objectreference was not set on an objectinstance" (or something like that
:-))
here's a sample code:
[Designer(typeof(MyControlDesigner))]
public class MyUserControl : System.Windows.Forms.UserControl
{
// ... some code here
private Panel contentPanel;
public Panel ContentPanel
{
get { return this.contentPanel; }
}
}
public class MyControlDesigner : System.Windows.Forms.Design.ControlDesigner
{
public override void Initialize(IComponent component)
{
base.Initialize (component);
IDesignerHost designerHost =
(IDesignerHost)GetService(typeof(IDesignerHost));
// the designer create a new panel on the form
Panel panelInside = (Panel)designerHost.CreateComponent(typeof(Panel));
((MyUserControl)this.Control).ContentPanel.Controls.Add(panel);
}
}
it would be great if someone could help!
--------------------------------
From: Andr? Kroll
John Saunders - 12 Dec 2004 21:54 GMT
> hi!
>
[quoted text clipped - 24 lines]
> objectreference was not set on an objectinstance" (or something like that
> :-))
Have you tried running in the debugger and turning on a Break when that
exception is raised?
John Saunders
joeycalisay - 13 Dec 2004 01:43 GMT
> public class MyControlDesigner : System.Windows.Forms.Design.ControlDesigner
> {
[quoted text clipped - 8 lines]
> }
> }
The panel you created using CreateComponent (panelInside) and the
ContentPanel property of your MyUserControl (I think) should be the same
instance. On the last line of your designer's Initialize method, you were
adding an instance of Panel (panelInside) to the Control collection of the
ContentPanel property of your control which is null at that moment (I
believe so). It should be added to the Control collection of your
MyUserControl and then assigned to the value of your ContentPanel property.
I've encountered this design and there are a number of posts about this.
One problem is when you want to control/limit/restrict the dragging of other
controls to be on your ContentPanel not on the outer UserControl. In the
end, I discovered the non-client area programming of controls, which is
really what I wanted so the ContentPanel will only be the client area (where
controls are added) not on its border or title area (which is the defined
non-client area).