Click here to Skip to main content
15,888,908 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi all,
i've encountered a problem while trying to fill a UserControl
inside a panel.

i've created some UserControl's. my problem is that while trying to fill them in a panel, some are resizing as expected and some don't react and remain at the same size.


???

another thing - the controls whom react normally are PictrueBox type.
the other ones are Panels containing many controls.

thx!
Posted
Updated 11-Aug-17 6:13am

Hi,
It's an old post but anyway, I have experienced similar problems.

A set of my controls was not docking properly ( it was placed properly , but once I resized the form the controls stayed of the same size ( I was adding the controls at runtime). So , I actualy hadd to code a couple of methods to resize the whole control hierarchy.

C#
private static void ResizeChildsInt(Control c,bool recursive) {
      Rectangle r = c.DisplayRectangle;

      foreach (Control item in c.Controls) {
        if (item.Dock == DockStyle.Top) {
          item.Top = r.Top;
          item.Left = r.Left;
          item.Width = r.Width;
          if (item.Height > r.Height) {
            item.Height = r.Height;
          }
          r = new Rectangle(r.X, r.Y + item.Height, r.Width, r.Height - item.Height);
        } else if (item.Dock == DockStyle.Bottom) {
          int bottom = r.Bottom - item.Height;
          if (item.Height > r.Height) {
            item.Height = r.Height;
          }          
          item.Top = item.Height - r.Bottom;
          item.Left = r.Left;
          item.Width = r.Width;
          r = new Rectangle(r.X, r.Y, r.Width, r.Height - item.Height);
        } else if (item.Dock == DockStyle.Left) {
          int width = item.Width;
          if (width > r.Width) {
            width = r.Width;
          }
          item.Top = r.Top;
          item.Left = r.Left;
          item.Height = r.Height;
          item.Width = width;
          r = new Rectangle(r.X + item.Width, r.Y, r.Width - item.Width, r.Height);
        } else if (item.Dock == DockStyle.Right) {
          int width = item.Width;
          if (width > r.Width) {
            width = r.Width;
          }

          item.Top = r.Top;
          item.Left = r.Right - item.Width;
          item.Height = r.Height;
          item.Width = width;
          r = new Rectangle(r.X, r.Y, r.Width - item.Width, r.Height);
        }
      }
      //Fill
      foreach (Control item in c.Controls) {
        if (item.Dock == DockStyle.Fill) {
          item.Top = r.Top;
          item.Left = r.Left;
          item.Width = r.Width;
          item.Height = r.Height;
        }
      }

      //Recursive
      if (recursive) {
        foreach (Control item in c.Controls) {                
          if (item is UserControl == false) {
            ResizeChildsInt(item, recursive);
          }
        }
      }
    }

    public static void ResizeChilds(Control c) {
      c.SuspendLayout();
      ResizeChildsInt(c, true);
      c.ResumeLayout();
    }

Just call the static method ResizeChilds each time the control is resized.
 
Share this answer
 
v2
Comments
[no name] 28-Oct-11 13:43pm    
Good gawd man let it die!
Start your own thread.
For one the Op has likely moved on. For two you can not 'accept' and answer. For three it is almost 2 years old!
Armando de la Torre 4-Nov-11 15:33pm    
I actually found the article because I had the same question. So I bookmarked it. Once I found the answer I posted it. Maybe it will help someone in the future.
phil.o 28-Oct-11 15:47pm    
Would call that 'topic digging' :)
What have you set those panels to do when they are docked ? I'd suggest they are doing what they are told.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900