Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Good day all!!!

I have a UserControl that I created with the following hierarchy:

UserControl
- PictureBox
- PictureBox
- PictureBox
- PictureBox
- PictureBox
- PictureBox
- PictureBox

I have a background image in the UserControl(SizeMode = Zoom, so that background image grow/shrinks according), and I'm aligning my PictureBoxes (SizeMode = Zoom, so that image grow/shrinks according) to certain points on the background image in the UserControl. If add my UserControl to a Form it appears just the way I want it, with the PictureBoxes in right places.
However, when I set my UserControl's 'Dock' property to 'Fill', the background image is resize correctly, but the PictureBoxes are not resize or repositioned.

How do I go about resizing/repositioning/scaling the PictureBoxes so that the have the right sizes and their locations on the UserControl's background image.
Posted
Comments
Hitesh_Mistry 12-Dec-13 5:02am    
is it a winform application?
BillWoodruff 12-Dec-13 6:10am    
A key question here is: what about the aspect ratios of the content displayed in the PictureBoxes ? Do you need to limit their re-sizing so aspect ratio is maintained ? You imply that your BackGround Image does not need to be adjusted to maintain aspect ratio.

What assumptions, if any, can you make about the containing UserControl being re-sized at run-time ?
azinyama 12-Dec-13 7:12am    
BillWoodruff, Yes I would like to maintain the PictureBoxes aspect ratio. The UserControl and the PictureBox SizeModes are set to Zoom so the UserControl's image and the PictureBoxes images will resize automatically, if my understanding is correct.
BillWoodruff 12-Dec-13 7:28am    
Yes, PictureBoxSizeMode.Zoom will maintain aspect ratio: whether you'll be "happy" with what's displayed ... ?

Is there any "common feature" of your content that will be displayed in the PictureBoxes ? For example, a common width, or height ? Are they arranged in any regular order ? Are they movable at run-time ?

Can the Form be re-sized smaller, as well as larger ?

And, right now, what Container are the PictureBoxes displayed in: a Form ? A Panel ? or ... ?
azinyama 12-Dec-13 7:47am    
Here is what I'm trying to achieve. I have an image of a 4 way intersection (square in shape), which I'm using as the background from my UserControl (SizeMode = Zoom). Then for each road; a picturebox showing cars in the lanes, a picture box that has an arrow pointing in the traffic flows direction, and to other picture boxes that represent pedestrian go and stop. Their positioning is in such as way that I can't use tablelayoutpanels etc flowlayoutpanel. For all four roads pictureboxes for cars are the same size, for arrows same size, and so on.

The UserControl is what I then what to drop on to my form, into a tablelayoutpanel on my form; and docked.

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:
C#
// Dictionaries to hold the ratios of PictureBox
// Left and Top to their Panel Container
// Width, and Height
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)
        {
            // to avoid an integer result from the division
            // at least one of two values must be converted
            // to Type Double
            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.
 
Share this answer
 
Comments
azinyama 19-Dec-13 12:43pm    
Ok. Thanx. Will wait to here from you
I don't think there's any automatic magic that does what you want. I feel like this is what AnchorMode.None should do, but it isn't (I've not yet found the scenario where that value is useful).

You'll need to add resize behaviour to the user control (override OnResize ... make sure you call the base implementation otherwise really weird things start happening) that adjusts the position and size of the PictureBoxes manually.
 
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