all groups > dotnet windows forms designtime > may 2006 >
You're in the

dotnet windows forms designtime

group:

How to control adding of a control


How to control adding of a control Peter Larsen
5/30/2006 6:54:42 PM
dotnet windows forms designtime:
Hi,

I have created an UserControl control, and added a designer class to it.
My problem is that i want to control where it is placed - it could be that i
only want my usercontrol to be added on a panel.
The following code shows how to control my usercontrol if it is dragged on a
form. This part works just fine.

public override bool CanBeParentedTo(IDesigner parentDesigner)
{
if (parentDesigner.Component is Panel)
return true;
else
return false;
}

But if i drag my usercontrol from the ToolBox and drop it on the form, the
above code does not prevent my usercontrol from being placed on the form.

I know how to control whether my usercontrol should allow childs or not :

void theControl_ControlAdded(object sender, ControlEventArgs e)
public override bool CanParent(Control control)

but this dont say anything about who is the parent.

How should i control adding a control from the ToolBox ??

Thanks in advance.
BR
Peter

Re: How to control adding of a control Gaurav Vaish (www.EduJini.IN)
5/30/2006 11:03:11 PM
Hi Peter,

[quoted text, click to view]

This method is never called when the item is dropped from the Toolbox. You
may not override this method.

[quoted text, click to view]

In the designer class (which must derive from ParentControlDesigner instead
of directly ControlDesigner), override the method "CanParent" method.
The functionality is similar to that of CanBeParentedTo but the reverse way
(CanParent instead of CanBeParentedTo).

Override both overloaded forms, in CanParent(ControlDesigner designer), you
may have just one line code:
CanParent(designer.Control);



--
Happy Hacking,
Gaurav Vaish
http://www.mastergaurav.org
http://webservices.edujini.in
-------------------



Re: How to control adding of a control Peter Larsen
5/31/2006 12:00:00 AM
Hi Gaurav Vaish,

Thanks for your comment.

CanParent control which controls that is dropped on itself. This is not what
i want here.
If i want to use CanParent, then i must add CanParent to all controls on the
form so it is capable of rejecting my UserControl picked from the ToolBox
(except where it is allowed).

I want CanBeParentedTo - and i'm already using it - but as you said,
CanBeParentedTo does not catch the event when you drop a control from the
TollBox.

So what am i going to do ??

CapParent and ControlAdded together catch all events related to adding a
control to itself.
CapBeParentedTo and "something unknown" together should be able to catch all
events when the control itself is being placed on a parent.

BR
Peter

Re: How to control adding of a control Peter Larsen
5/31/2006 12:00:00 AM
Hi,

I think i have found that ParentChanged does the trick:

void theControl_ParentChanged(object sender, EventArgs e)
{
if (theControl.Parent != null)
{
if (! (theControl.Parent is Panel) )
throw new ApplicationException("This control is not
allowed on " + theControl.Parent.ToString() + '\n');
}
}

BR
Peter

Re: How to control adding of a control Gaurav Vaish (www.EduJini.IN)
6/2/2006 1:07:00 AM
[quoted text, click to view]

Hmmm... should have thought about it long back.
Silly me. :D


--
Happy Hacking,
Gaurav Vaish
http://www.mastergaurav.org
http://www.edujini.in
http://webservices.edujini.in
-------------------

AddThis Social Bookmark Button