Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / Design Time / December 2004

Tip: Looking for answers? Try searching our database.

IDesignerHost + CreateComponent problem

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
the-savior - 10 Dec 2004 08:54 GMT
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).

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.