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 / June 2006

Tip: Looking for answers? Try searching our database.

How to control adding of a control

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Peter Larsen - 30 May 2006 17:54 GMT
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
Gaurav Vaish (www.EduJini.IN) - 30 May 2006 18:33 GMT
Hi Peter,

>        public override bool CanBeParentedTo(IDesigner parentDesigner)

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

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

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);

Signature

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

Peter Larsen - 31 May 2006 08:29 GMT
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
Peter Larsen - 31 May 2006 09:04 GMT
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
Gaurav Vaish (www.EduJini.IN) - 01 Jun 2006 20:37 GMT
>        void theControl_ParentChanged(object sender, EventArgs e)

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

Signature

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


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.