1 Replies - 6139 Views - Last Post: 08 July 2011 - 11:00 AM Rate Topic: -----

#1 kneil_20  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 53
  • Joined: 27-August 09

How to insert multiple pictureboxes on a panel

Posted 08 July 2011 - 09:45 AM

I have this code
public void makeBoxes(int size) // number of pictureboxes
        {
            for (int x = 0; x < size; x++)
            {
                PictureBox pic = new PictureBox();
                picList.Add(pic);
                
            }
            pix = picList.ToArray();
            
        }
 public void insertPix()
        {
            for (int x = 0; x < pix.Length; x++)
            {
                pix[x].BorderStyle = BorderStyle.FixedSingle;
                panel1.Controls.Add(pix[x]);
               
            }
        }


I don't know what the default value is for length and width of pictureboxes. Anyway I can't insert those pictureboxes on the panel1.

EDIT: ah nevermind I just forgot to execute the method... I figured out how to change the size in the loop and algorithm to make it arrange the boxes

This post has been edited by kneil_20: 08 July 2011 - 10:08 AM


Is This A Good Question/Topic? 1
  • +

Replies To: How to insert multiple pictureboxes on a panel

#2 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5441
  • View blog
  • Posts: 11,674
  • Joined: 02-June 10

Re: How to insert multiple pictureboxes on a panel

Posted 08 July 2011 - 11:00 AM

Quote

I don't know what the default value is for length and width of pictureboxes.


Sure you do: Drag a picturebox from the tools pallet to a form. That's the default size.

Some of this is ... odd. I don't understand why you are adding the picturebox to a global variable of some type (picList), which in turn you are then assigning to another global of pix.

You might want to get used to having less global variables and do more with returned objects.

        public Example()
        {
            InitializeComponent();
        }

        private void Example_Load(object sender, EventArgs e)
        {
            InsertBoxes(MakeBoxes(7));
        }

        private PictureBox[] MakeBoxes(int count)
        {
            var results = new PictureBox[count];
            for(int Index = 0; Index< count; Index++)
            {
                results[Index] = new PictureBox()
                                     {
                                         BorderStyle = BorderStyle.FixedSingle
                                     };
            }
            return results;
        }

        private bool InsertBoxes(PictureBox[] ThePictureBoxes)
        {
            bool successful = false;
            try
            {
                flpBrowser.Controls.AddRange(ThePictureBoxes); //flpBrowser is a FlowLayoutPanel
                successful = true;
            }
            catch (Exception err)
            {
                Console.WriteLine(err.Message);
            }
            return successful;
        }

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1