I have located the code I wrote a few years ago that handles the case of both re-sizing and re-positioning Controls within a ContainerControl, as the ContainerControl is re-sized proportionally as the Form it is in is re-sized, or the Form WindowState is changed.
However, this code was done for a client, and I can't re-post it here without their permission. I've sent a message asking their permission.
Here's a general outline of how to do this:
1. When the Application starts ... in the Main Form Load Event ... record the size of the Main Form and the size of the Panel.
2. Calculate ratios of the Panel Width to Form Width, and the Panel Height to Form Height.
3. Create Dictionaries of Types <Control, double> to hold the ratios of PictureBox Control Left and Top Properties to the Panel's Left and Top Properties.
4. go through the Panel's Controls, and, for each Panel that's a PictureBox, calculate the ratios, and add the PictureBox (as Control) and the ratio to the Dictionaries.
5. In the Form SizeChanged EventHandler:
a. exit if the current WindowState is Minimized
b. update the size of the Form, calculate and store the new ratios of Form Width to Form Height.
c. call the code to re-size the Panel.
d. update Form size and bounds variables as necessary
6. In the code to re-size the Panel:
a. suspend the layout of the Panel
b. loop through all the Controls in the Panel and, for each one that's a PictureBox:
1. re-position it using the ratios in the Dictionaries
2. re-size it using the relative ratio of Panel size to Form size
c. resume the layout of the Panel
d. update variables as necessary
Here's a sample of what your initial code that prepares for dynamic re-sizing could look like:
private Dictionary<Control, double> XRatio = new Dictionary<Control, double>();
private Dictionary<Control, double> YRatio = new Dictionary<Control, double>();
private RectangleF currentFormDisplayRectangle;
private RectangleF currentPanelDisplayRectangle;
private void Form1_Load(object sender, EventArgs e)
{
currentFormDisplayRectangle = DisplayRectangle;
currentPanelDisplayRectangle = pnlForPictureBoxes.DisplayRectangle;
foreach (Control theControl in pnlForPictureBoxes.Controls)
{
if (theControl is PictureBox)
{
XRatio.Add(theControl, Convert.ToDouble(theControl.Left) / pnlForPictureBoxes.Width);
YRatio.Add(theControl, Convert.ToDouble(theControl.Top) / pnlForPictureBoxes.Height);
}
}
}
I hope this is useful in some way. If I can get permission, I'll post complete code.