Simple(ish) problem with a user control

J

James Crosswell

I have a very simple user control. I want this control to act as a sort
of container so that I could drag/drop other components (from the
toolbox) onto this control and then dock them etc within this user
control. What do I need to do to achieve this?

I can mimic the behavior that I want at runtime by setting properties
for child controls in constructors like:
ChildControl1.Parent = MyUserControl;
ChildControl2.Dock = DockStyle.Bottom;
ChildControl2.Parent = MyUserControl;
ChildControl2.Dock= DockStyle.Fill;

But I'd like to do this at design time simply by draging/dropping
ChildControl1 and ChildControl2 onto MyUserControl and setting the
appropriate Dock properties.

Thanks in advance.

Best Regards,

James Crosswell
Microforge.net LLC
http://www.microforge.net
 
O

Oliver Sturm

Hello James,
I have a very simple user control. I want this control to act as a sort of
container so that I could drag/drop other components (from the toolbox)
onto this control and then dock them etc within this user control. What do
I need to do to achieve this?

The easiest way to do that would be to use a ContainerControl as the base
class of your control, instead of the UserControl. UserControl actually
derives from ContainerControl, but "switches off" the behaviour that
allows for other controls to be dropped onto it at design time. It does
this by activating its own designer for the UserControl, which doesn't
take dropped controls.

Alternatively you can use the following attribute on your UserControl
derived control, thereby switching the design time support for your
control back to that of the ScrollableControl (further up the class
hierarchy).

[Designer("System.Windows.Forms.Design.ScrollableControlDesigner, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]



Oliver Sturm
 
J

James Crosswell

Oliver said:
Hello James,
I have a very simple user control. I want this control to act as a
sort of container so that I could drag/drop other components (from the
toolbox) onto this control and then dock them etc within this user
control. What do I need to do to achieve this?

The easiest way to do that would be to use a ContainerControl as the
base class of your control, instead of the UserControl. UserControl
actually derives from ContainerControl, but "switches off" the behaviour
that allows for other controls to be dropped onto it at design time. It
does this by activating its own designer for the UserControl, which
doesn't take dropped controls.

Alternatively you can use the following attribute on your UserControl
derived control, thereby switching the design time support for your
control back to that of the ScrollableControl (further up the class
hierarchy).

[Designer("System.Windows.Forms.Design.ScrollableControlDesigner,
System.Design, Version=2.0.0.0, Culture=neutral,
PublicKeyToken=b03f5f7f11d50a3a")]

Fantastic - thanks Oliver! In my case (for complex reasons) I have to
use a UserControl in this particular case but adding the Designer
attribute to override the designer used for the UserControl worked.

Thank you very much!

Best Regards,

James Crosswell
Microforge.net LLC
http://www.microforge.net
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top