Click here to Skip to main content
15,892,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, How do I dynamically add the PictureBox control side by side with a margin?
Preference: Could it be possible to link the size of the margin in pixels to a textbox's input?
Posted
Updated 21-Dec-13 1:11am
v2
Comments
Sampath Lokuge 21-Dec-13 2:18am    
Is this windows or asp.net forms or mvc ?
IAmABadCoder 21-Dec-13 7:08am    
Windows forms
BillWoodruff 21-Dec-13 3:19am    
1. are the PictureBoxes to be added the same size, or different sizes ?

2. what do you mean by side-to-side: from top to bottom, and from left to right ? ... or ?

3. are the PictureBoxes added at run-time, or design-time ?
IAmABadCoder 21-Dec-13 7:08am    
1.) Same size
2.) Left to right
3.) At runtime
BillWoodruff 21-Dec-13 10:51am    
Have you tried the code provided by Alexander S. below ?

1 solution

I assume that you are using WinForm
I have created DemoForm and added on it FlowLayoutPanel (panel) and NumericUpDown (numericUpDownMargin)
here is some code
C#
public partial class DemoForm : Form
    {
        private List<PictureBox> pictures;
        private int n = 16;
        public DemoForm()
        {
            InitializeComponent();
            // then form is resing, panel is also resizing and all picture boxes will change their location
            panel.Anchor = AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right;
            numericUpDownMargin.ValueChanged += MarginValueChanged;

            pictures = new List<PictureBox>();
            // adding picture boxes with images
            for(int i=0; i<n; i++)
            {
                var picture = new PictureBox();
                var bmp = new Bitmap(@"path to your image");
                picture.Image = bmp;
                picture.Size = new Size(150,150);
                pictures.Add(picture);
                panel.Controls.Add(picture);
                picture.Visible = true;
            }
        }

        // changing margin of all pictures boxes
        private void MarginValueChanged(object sender, EventArgs e)
        {
            int margin = (int) numericUpDownMargin.Value;
            panel.SuspendLayout();
            foreach (var pictureBox in pictures)
                pictureBox.Margin = new Padding(margin);

            panel.ResumeLayout();
        }
    }
 
Share this answer
 
v2
Comments
BillWoodruff 21-Dec-13 10:52am    
+5 !

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