Thread: can i create a picturebox from an array?

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    9

    Question can i create a picturebox from an array?

    i want to set an image to a picturebox via a file as follows:

    name of the picture , picturebox name, x , y , pos x, pos y

    the loading of the textfile works great and all. but i cant seem to create a picturebox with the name of the one specified in the textfile... so how can i do that?

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    You could if the name in the textfile didn't contain any of the characters prohibited in an identifier. What name are you trying to use?
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    Sep 2011
    Posts
    9
    the names pictureBox1 pictureBox2 3 4... etc etc...

    i've made this piece of code:

    Code:
                if (MyImage != null)
                {
                    MyImage.Dispose();
                }
                PictureBox[] pictureBox = new PictureBox[5];
                pictureBox[pic] = new PictureBox();
                pictureBox[pic].Name = pictureboxes[pic];
                MyImage = new Bitmap(file);
                pictureBox[pic].Location = new Point(locx, locy);
                pictureBox[pic].ClientSize = new Size(xsize, ysize);
                pictureBox[pic].Image = (Image)MyImage;
                pic++;
    but it gives me nothing, the pictureboxes remain empty.

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Make sure you're adding the picturebox its container's controls list:

    Assuming this is in your main form's code file:
    Code:
    this.Controls.Add(pictureBox[pic]);
    If you understand what you're doing, you're not learning anything.

  5. #5
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    I would recommend using List<> in System.Collections.Generic instead of the array. This would remove the hard-coded [5] in your code.

  6. #6
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Quote Originally Posted by VirtualAce View Post
    I would recommend using List<> in System.Collections.Generic instead of the array. This would remove the hard-coded [5] in your code.
    Seems like unnecessary overhead if he knows there's ever only going to be 5 PictureBoxes. Why would you recommend using a List<> instead?
    If you understand what you're doing, you're not learning anything.

  7. #7
    Registered User
    Join Date
    Sep 2011
    Posts
    9
    ah, actually i was trying to make a little puzzle game with it. now that i understand XNA a bit, i´m using sprites with indeed a list<>. anyway... thanks for all the help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C# and PictureBox not loading
    By martind1 in forum C# Programming
    Replies: 2
    Last Post: 02-02-2011, 09:47 AM
  2. create and populate create bidimensional array
    By darkducke in forum C Programming
    Replies: 0
    Last Post: 12-03-2010, 07:06 AM
  3. PictureBox
    By ingenting in forum Windows Programming
    Replies: 2
    Last Post: 06-20-2010, 06:49 AM
  4. vc++ picturebox array
    By Crypteasy in forum C++ Programming
    Replies: 3
    Last Post: 01-09-2008, 08:19 PM
  5. picturebox and openfiledialog
    By BraneMxm in forum C# Programming
    Replies: 1
    Last Post: 06-15-2007, 10:32 AM