Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. .NET (Core and Framework)
  4. Dynamic picturebox allocation

Dynamic picturebox allocation

Scheduled Pinned Locked Moved .NET (Core and Framework)
csharpgraphicshelptutorialannouncement
5 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U Offline
    U Offline
    Umangj
    wrote on last edited by
    #1

    I am creating n pictureboxes and then setting a different image on each one of them in a loop. The user clicks on open n opens a .zip file (containing images only). I am unzipping the .zip contents in a folder and am able to extract the images from there.For the first time the pictureboxes rightly shows the images. But when again user click on the open button and select a .zip file to be opened. The new images doesnt appear completely. Problem could be: previous images are not getting removed so are not allowing the new images to appear. Language: c# code:

    pb = new PictureBox[r*c];
    Point point = new Point(100, 100);

            //pb.Image = null;
    
            for (i = 0; i < r \* c; i++)
            {
                pb\[i\] = new PictureBox();
               
                {
                    pb\[i\].Dispose();
                }
                
                pb\[i\].Image = null;
            }
            int w = 800 / r;
            int k = 0;
            int h = 700 / c;
            int a = w, b = h;
            for (i1 = 0; i1 < r; i1++)
            {
                b = 15;
                for (i2 = 0; i2 < c; i2++)
                {
                    
                   // this.TransparencyKey = Color.Transparent;
                    //pb.BackColor = Color.Transparent;
                    pb\[k\] = new PictureBox();
                    pb\[k\].Image = null;        
                    pb\[k\].Size = new Size(w, h);
                    this.Controls.Add(pb\[k\]);
                    if (i1 == 0 )
                    {
                        pb\[k\].Location = new Point(10, b);
                    }
                    else
                    {
                        pb\[k\].Location = new Point(a, b);
                    }
                    //a = a + w;
                    b = b + h;
                    Bitmap img=new Bitmap("d:\\\\puzzlesolver\\\\pieces\\\\piece"+i2.ToString()+i1.ToString()+".png");
                    Bitmap img1 = new Bitmap(img);
                    pb\[k\].Image = img1;
                    pb\[k\].SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom;
                    pb\[k++\].Update();
                    this.Update();
                   // img.Dispose();
                    
                }
                a = a + w;
            }
            this.Update();
    

    I have tried a lot of things please tell me how to remove the existing picture boxes which are dynamically allocated and then add them ag

    U G 2 Replies Last reply
    0
    • U Umangj

      I am creating n pictureboxes and then setting a different image on each one of them in a loop. The user clicks on open n opens a .zip file (containing images only). I am unzipping the .zip contents in a folder and am able to extract the images from there.For the first time the pictureboxes rightly shows the images. But when again user click on the open button and select a .zip file to be opened. The new images doesnt appear completely. Problem could be: previous images are not getting removed so are not allowing the new images to appear. Language: c# code:

      pb = new PictureBox[r*c];
      Point point = new Point(100, 100);

              //pb.Image = null;
      
              for (i = 0; i < r \* c; i++)
              {
                  pb\[i\] = new PictureBox();
                 
                  {
                      pb\[i\].Dispose();
                  }
                  
                  pb\[i\].Image = null;
              }
              int w = 800 / r;
              int k = 0;
              int h = 700 / c;
              int a = w, b = h;
              for (i1 = 0; i1 < r; i1++)
              {
                  b = 15;
                  for (i2 = 0; i2 < c; i2++)
                  {
                      
                     // this.TransparencyKey = Color.Transparent;
                      //pb.BackColor = Color.Transparent;
                      pb\[k\] = new PictureBox();
                      pb\[k\].Image = null;        
                      pb\[k\].Size = new Size(w, h);
                      this.Controls.Add(pb\[k\]);
                      if (i1 == 0 )
                      {
                          pb\[k\].Location = new Point(10, b);
                      }
                      else
                      {
                          pb\[k\].Location = new Point(a, b);
                      }
                      //a = a + w;
                      b = b + h;
                      Bitmap img=new Bitmap("d:\\\\puzzlesolver\\\\pieces\\\\piece"+i2.ToString()+i1.ToString()+".png");
                      Bitmap img1 = new Bitmap(img);
                      pb\[k\].Image = img1;
                      pb\[k\].SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom;
                      pb\[k++\].Update();
                      this.Update();
                     // img.Dispose();
                      
                  }
                  a = a + w;
              }
              this.Update();
      

      I have tried a lot of things please tell me how to remove the existing picture boxes which are dynamically allocated and then add them ag

      U Offline
      U Offline
      Umangj
      wrote on last edited by
      #2

      heyy, please reply to this question guys................

      P 1 Reply Last reply
      0
      • U Umangj

        I am creating n pictureboxes and then setting a different image on each one of them in a loop. The user clicks on open n opens a .zip file (containing images only). I am unzipping the .zip contents in a folder and am able to extract the images from there.For the first time the pictureboxes rightly shows the images. But when again user click on the open button and select a .zip file to be opened. The new images doesnt appear completely. Problem could be: previous images are not getting removed so are not allowing the new images to appear. Language: c# code:

        pb = new PictureBox[r*c];
        Point point = new Point(100, 100);

                //pb.Image = null;
        
                for (i = 0; i < r \* c; i++)
                {
                    pb\[i\] = new PictureBox();
                   
                    {
                        pb\[i\].Dispose();
                    }
                    
                    pb\[i\].Image = null;
                }
                int w = 800 / r;
                int k = 0;
                int h = 700 / c;
                int a = w, b = h;
                for (i1 = 0; i1 < r; i1++)
                {
                    b = 15;
                    for (i2 = 0; i2 < c; i2++)
                    {
                        
                       // this.TransparencyKey = Color.Transparent;
                        //pb.BackColor = Color.Transparent;
                        pb\[k\] = new PictureBox();
                        pb\[k\].Image = null;        
                        pb\[k\].Size = new Size(w, h);
                        this.Controls.Add(pb\[k\]);
                        if (i1 == 0 )
                        {
                            pb\[k\].Location = new Point(10, b);
                        }
                        else
                        {
                            pb\[k\].Location = new Point(a, b);
                        }
                        //a = a + w;
                        b = b + h;
                        Bitmap img=new Bitmap("d:\\\\puzzlesolver\\\\pieces\\\\piece"+i2.ToString()+i1.ToString()+".png");
                        Bitmap img1 = new Bitmap(img);
                        pb\[k\].Image = img1;
                        pb\[k\].SizeMode = System.Windows.Forms.PictureBoxSizeMode.Zoom;
                        pb\[k++\].Update();
                        this.Update();
                       // img.Dispose();
                        
                    }
                    a = a + w;
                }
                this.Update();
        

        I have tried a lot of things please tell me how to remove the existing picture boxes which are dynamically allocated and then add them ag

        G Offline
        G Offline
        Gideon Engelberth
        wrote on last edited by
        #3

        for (i = 0; i < r * c; i++)
        {
        pb[i] = new PictureBox();
        {
        pb[i].Dispose();
        }

        pb\[i\].Image = null;
        

        }

        This is almost certainly not doing what you expect. You are creating new PictureBoxes and immediately Disposing them.

        this.Controls.Add(pb[k]);

        Every control added to a Form stay on the form until you call this.Controls.Remove with the same control. Disposing the control before removing it from the form may cause an exception, so make sure you do it in the right order.

        U 1 Reply Last reply
        0
        • U Umangj

          heyy, please reply to this question guys................

          P Offline
          P Offline
          Paul Conrad
          wrote on last edited by
          #4

          You should not push people to reply to your question. Either the person who knows what is the problem hasn't seen it yet, or nobody knows. Exercise a little patience.

          "The clue train passed his station without stopping." - John Simmons / outlaw programmer "Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon "Not only do you continue to babble nonsense, you can't even correctly remember the nonsense you babbled just minutes ago." - Rob Graham

          1 Reply Last reply
          0
          • G Gideon Engelberth

            for (i = 0; i < r * c; i++)
            {
            pb[i] = new PictureBox();
            {
            pb[i].Dispose();
            }

            pb\[i\].Image = null;
            

            }

            This is almost certainly not doing what you expect. You are creating new PictureBoxes and immediately Disposing them.

            this.Controls.Add(pb[k]);

            Every control added to a Form stay on the form until you call this.Controls.Remove with the same control. Disposing the control before removing it from the form may cause an exception, so make sure you do it in the right order.

            U Offline
            U Offline
            Umangj
            wrote on last edited by
            #5

            Thnk you for replying..... i was successfully able to handle the problem that i was facing

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups