Ryan
Ryan

Reputation: 749

How can I move all the controls in a panel on form resize?

I'm playing around with a project, C# .NET 4, and I'm kind of stuck. I have with a panel that will be dynamically filled with checkboxes based on the number of items in a folder, which works fine.

But when I resize the form I want the checkboxes to move in the panel, like a little grid of checkboxes that takes up what space it can.

Thus far I have been unsuccessful in finding an efficient way to do this. I still haven't gotten it to work flat out yet, but I don't know that it matters. The methods I'm trying are resource killers.

ie.

int boxCount = panel1.Size.Width/123;
int x = 3, y = 3, i = 0;

foreach (Control chkbox in panel1.Controls)
{

      if (i < boxCount)
      {
            chkbox.Location = new Point(x, y);
            x += 123;
      }
      else
      {
            i = 0;
            x = 3;
            y += 123;
            chkbox.Location = new Point(x, y);
      }

      i++;
}

I'm looking for something that doesn't put my CPU in a choke hold and turn my form into a 3 page flip book when I resize it. For reference, I had something like the Windows desktop wallpaper selector in 7 in mind, minus the resizing of the actual controls.

I've not worked with WPF, but I am likely to start having been shown the light, so I was looking a garden variety Windows Forms solution.

Upvotes: 1

Views: 4891

Answers (2)

David
David

Reputation: 73554

Would a TableLayoutPanel or a FlowLayoutPanel meet your needs?

Upvotes: 1

Jacob
Jacob

Reputation: 78850

It sounds like you're looking for the FlowLayoutPanel.

Upvotes: 4

Related Questions