Change the size of the panel dynamically

Dinu picture Dinu · Mar 10, 2010 · Viewed 70.8k times · Source

I am implementing an application that needs to drag and drop image boxes in a panel.Image boxes are added dynamically from the program and so I have set the autoscroll property to true in panel.But when I have drag out the boxes at bottom in the panel the panel size got reduced.I have put autosize property false in panel.The panel is docked in another panel.I want to set the size of panel at run time.How can I achieve this.

public form1(int[,] dummy, int columnSize, int rowSize)
   {
       this.dummy= dummy;
       numOfColumns = columnSize;
        numOfRows = rowSize;
        getData();
        addIds = addIdArray;
        data = mylist;
        InitializeComponent();
        //panel1.MinimumSize = new Size(columnSize * 40, rowSize * 40);
        //panel1.Height = rowSize * 40;
        //panel1.Width = columnSize * 40;
        //panel4.Height = rowSize * 40;
        //panel4.Width = columnSize * 40;
        int x, y;
        Structures.EmptyRectSpace space = new Structures.EmptyRectSpace();
        for (int i = 0; i < data.Count; i++)// set picture boxes 
        {
            space = (Structures.EmptyRectSpace)data[i];
            x = space.startingJ;
            y = space.startingI;
            int h, w;
            h = space.length;
            w = space.width;

             p = new PictureBox();
                p.Width = w * 40;
                p.Height = h * 40;
                p.BackColor = Color.DarkGreen;
                p.Image = Properties.Resources.v;
                p.BorderStyle = BorderStyle.FixedSingle;
                p.Name = addIdArray[i].ToString();
                p.Location = new Point((x + 1 - w) * 40, (y + 1 - h) * 40);

                this.panel1.Controls.Add(p);
        }

        foreach (Control c in this.panel1.Controls)
        {
            if (c is PictureBox)
            {
                c.MouseDown += new MouseEventHandler(pictureBox1_MouseDown);
            }
        }
        this.panel1.DragOver += new System.Windows.Forms.DragEventHandler(this.panel1_DragOver);
        panel1.DragOver += new DragEventHandler(panel1_DragOver);
        panel1.DragDrop += new DragEventHandler(panel1_DragDrop);
        panel1.AllowDrop = true;
        panel2.AllowDrop = true;
        foreach (Control c in this.panel2.Controls)
        {
            c.MouseDown += new MouseEventHandler(pictureBox1_MouseDown);
        }
        this.panel2.DragOver += new System.Windows.Forms.DragEventHandler(this.panel2_DragOver);
        panel2.DragOver += new DragEventHandler(panel2_DragOver);
        panel2.DragDrop += new DragEventHandler(panel2_DragDrop); 
    }

This is the constructor of the form that contained panel. When it loaded the picture boxes must be added to the panel and there drag drop events of panel are implemented.

Please give me a helping hand..

Answer

Jason picture Jason · May 7, 2012

I couldn't get this to work unless I set the max width programmatically in code. It didn't matter what the max width was in the designer (or if max width was even set).

int newWidth = 200;
panel.MaximumSize = new Size(newWidth, panel.Height);
panel.Size = new Size(newWidth, panel.Height);