• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2395
  • Last Modified:

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.
0
ArnarFreyr
Asked:
ArnarFreyr
  • 5
  • 3
1 Solution
 
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 new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

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

Featured Post

Take Control of Web Hosting For Your Clients

As a web developer or IT admin, successfully managing multiple client accounts can be challenging. In this webinar we will look at the tools provided by Media Temple and Plesk to make managing your clients’ hosting easier.

  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now