Custom container control with limited container region

I am trying to create a custom container control for Windows Forms
using C#.NET 2.0. My problem is that I want only part of the control
to allow other controls to be placed on it at design time. A similar
example to what I'm trying to accomplish is a Tab Control. The tab
pages of a tab control allow child controls to be added to the page
region, but not to the tab. Does anyone know how to accomplish this?
In either c# or vb.net.
ArnarFreyrAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

WarpsehCommented:
An easy solution would be to add a container (like a panel control) inside yours and set it's location and size to the boundaries you need, then, set your control's Controls property like this:
public ControlsCollection Controls
{
  get {
      return this.panel.Controls;
  }
  set
  {
     this.panel.Controls = value;
  }
}

Open in new window

0
ArnarFreyrAuthor Commented:
what namespace should I use?
0
ArnarFreyrAuthor Commented:
You mean ControlCollection not ControlsCollection?

I get the error:
Property or indexer 'System.Windows.Forms.Control.Controls' cannot be assigned to -- it is read only
0
The Ultimate Tool Kit for Technolgy Solution Provi

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy for valuable how-to assets including sample agreements, checklists, flowcharts, and more!

WarpsehCommented:
Sorry, you were right, that wasn't working... Try this one, I have tested it and it seems to do it right.
public partial class LimitedContainer : ContainerControl
    {
        public LimitedContainer()
        {
            InitializeComponent();
 
            //Sets the bounds for the controls (in this case, it lefts a space of 30px in all sides
            ControlBounds = new Rectangle(30, 30, Width - 60, Height - 60);
            this.Layout += new LayoutEventHandler(LimitedContainer_Layout);
        }
 
        public Rectangle ControlBounds;
 
        void LimitedContainer_Layout(object sender, LayoutEventArgs e)
        {
            //Checks for each control if it is outside the bounds and removes it. 
            //you could modify this function if you want to let certain controls 
            //to be outside the bounds, like in a tab control.
            foreach (Control c in Controls)
            {
                int x, y;
                x = c.Bounds.X;
                y = c.Bounds.Y;
                if (c.Location.X < ControlBounds.X)
                    x = ControlBounds.X;
                if (c.Location.Y < ControlBounds.Y)
                    y = ControlBounds.Y;
                if (c.Location.X + c.Width > ControlBounds.Right)
                    x = ControlBounds.Right - c.Bounds.Width;
                if (c.Bounds.Bottom > ControlBounds.Bottom)
                    y = ControlBounds.Bottom - c.Bounds.Height;
                c.Location = new Point(x, y);
            }
        }
    }

Open in new window

0
WarpsehCommented:
I've fixed a little the layout event handler, there was quite a mix of bounds and locations. this one is a little bit more clearer, only using bounds for checking the borders:
public partial class LimitedContainer : ContainerControl
{
    public LimitedContainer()
    {
        InitializeComponent();
        //Sets the bounds for the controls (in this case, it lefts a space of 30px in all sides
        ControlBounds = new Rectangle(30, 30, Width - 60, Height - 60);
        this.Layout += new LayoutEventHandler(LimitedContainer_Layout);
    }
 
    public Rectangle ControlBounds;
 
    void LimitedContainer_Layout(object sender, LayoutEventArgs e)
    {
        //Checks for each control if it is outside the bounds and removes it. 
        //you could modify this function if you want to let certain controls 
        //to be outside the bounds, like in a tab control.
        foreach (Control c in Controls)
        {
            int x, y;
            x = c.Bounds.X;
            y = c.Bounds.Y;
            if (c.Bounds.X < ControlBounds.X)
                x = ControlBounds.X;
            if (c.Bounds.Y < ControlBounds.Y)
                y = ControlBounds.Y;
            if (c.Bounds.Right > ControlBounds.Right)
                x = ControlBounds.Right - c.Bounds.Width;
            if (c.Bounds.Bottom > ControlBounds.Bottom)
                y = ControlBounds.Bottom - c.Bounds.Height;
            c.Location = new Point(x, y);
        }
    }
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ArnarFreyrAuthor Commented:
thanks, but this doesn't keep the control bounded to parent control. For e.x if i dock an object the object
doesn't follow the rule of my declared container.
0
ArnarFreyrAuthor Commented:
I give you the points for th code you supplied thought it did not complete what i was looking for, but thx anyway.
0
ArnarFreyrAuthor Commented:
You did not provide me with a complete solution, it is a partially solution, thats why i give you the points.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.