[.net] Adding controls to a UserControl's child

Started by
8 comments, last by Barguast 19 years, 8 months ago
I've got a UserControl which contains a Panel. Once this UserControl has been placed on a form (at design time), I want to be able to add further controls to the UserControl's Panel - not to the UserControl itself. Does anyone know how I can do this? If I've not been clear enough, then say so and I'll rephrase.
Using Visual C++ 7.0
Advertisement
AFAIK this cannot be done on the Page designer, but should be able to be done via the User Controls designer.

This makes sense because the controls inside the User Control's Panel are specific to the User Control itself and not the Page hosting the User Control.

Hope this make sense.
I make sense, but it not the reply I was hoping for! If you can only add controls to the UserControl designer then it kind of ruins my whole idea

Someone has suggested that I create a custom designer which will do this for me, but there seems to be very little information on how to do this.

Maybe I'm just looking at the problem wrong - what I'm trying to do is allow controls to be placed on the UserControl but only in specfic locations - the reason why is that I'm trying to paint a border around the control but any other controls that are added to it get placed on top of the border.

Is there any other way I can solve this problem?
Using Visual C++ 7.0
I'm not sure about building the areas directly in to the control, but you could use hittestinfo and the mouseup event to determine if the place where the user has clicked is a valid place to drop a control. If not, don't allow it.
oh hai
Deriving your own Designer class is really easy and you can override the CanParent method to decide which controls can be dropped on your designer. So if I understood your problem correctly, it would look something like this:

public class MyUserControlDesigner : System.Windows.Forms.Design.ScrollableControlDesigner {		    public override bool CanParent(System.Windows.Forms.Control control) {        // naive version, will fail if scrollbars are visible..        return control is System.Windows.Forms.Panel && this.Control.Controls.Count == 0;    }}[Designer(typeof(MyUserControlDesigner))]public class MyUserControl {    //...}


You might also want to override the Desiger's OnPaintAdornments method for drawing a border around the control. You could also remove unnecessary properties by overriding the PreFilterProperties method.

Here's a quick introduction to designers.
Thanks for the link - I will read through it, but I want to make sure you've understood the problem correctly.

Lets say, for example, that I want to create a UserControl that can parent other controls - I'd derive a class from UserControl, which uses the ParentControlDesigner or one of the derived designers.

However, lets say that now I want it to have a 5-pixel thick border around the perimeter of the control. None of the controls dropped into this UserControl (in the Form designer)should be visible in this border. Would creating a new designer be the solution to achieving this?

As I said, by first approach to this problem was have the UserControl contain a panel and force all controls dropped into the UserControl become children of that panel - I tried overriding OnControlAdded but this seemed to go nowhere.

Does this better explain my problem? Thanks again.



Using Visual C++ 7.0
Ok sorry, I misunderstood your problem. In that case, I think would also derive my own designer class, and override its OnDragOver and OnDragDrop methods.. In OnDragOver you can check for the mouse position and set the cursor depending on the region, and in OnDragDrop you can do with the control whatever you like, I think that should work..
I'm still not sure that is a good solution. What if I added a control to the UserControl and then resized it? The obvious answer is to resize the child controls so they don't appear in the border, but this seems a little too basic. I'm trying to find a solution whereby the controls would actually be clipped by the border.

The only control that I've seen that does anything like what I'm trying to do is the TabControl. Once a tab has been added, you can see what looks like a panel on the TabPage which defines the area in which the child controls are visible. Does anyone know how it does this?

Also, most controls have a Borderstyle property already - this border is painted over the child controls too, something I've been unable to manage - even something like this would go a long way to solving this problem. Does anyone know how I alos paint a border in this way?

I see a lot of solutions to this problem, but I've got no idea which is the best or how to implement it :
But, thanks for your help. Hopefully you can see my problem with your solution. Any more assistance would be greatly appreciated.
Using Visual C++ 7.0
I don't think it's possible to draw the border on top of the child controls, but as you said there are various other possibilities. The easiest one I can think of would be to have 4 coloured, textless Labels docked to each edge of your Control and make sure they stay on top when other Controls are added. Not exactly pretty, but it would work....
The labels idea isn't so bad - a little messy maybe, but I think I'll go with that for now.

Thanks for your help.
Using Visual C++ 7.0

This topic is closed to new replies.

Advertisement