Click here to Skip to main content
15,891,184 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have an array of Picturebox in C#, how can I delete a picturebox if the user press the "del" key?

here's how far I got:

C#
private void Principal_KeyDown(object sender, KeyEventArgs e)
     {
         if (e.KeyCode == Keys.Delete)
         {
picbox[i].Dispose();
         }


     }


Now, how I get the i value (that's the selected image in the array?

When I create the picturebox array, I do this:
C#
picbox[i].Tag= i;


I tried this:

C#
string nroimgseleccionada = ((System.Windows.Forms.PictureBox)sender).Tag.ToString();
       int i = Int32.Parse(nroimgseleccionada);

but I get this:
Unable to cast object of type 'WindowsFormsApplication1.Principal' to type 'System.Windows.Forms.PictureBox'.

Thanks!
Posted

Ok, so I solved it by declaring a variable (public) and then in the click event for the picbox:

C#
private void clickpicbox(object sender, System.EventArgs e)
      {

          selectedimage = ((System.Windows.Forms.PictureBox)sender).Tag.ToString();
          

      }


then:
C#
private void Principal_KeyDown(object sender, KeyEventArgs e)
       {
           if (e.KeyCode == Keys.Delete)
           {

               int i = Int32.Parse(selectedimage);
               picbox[i].Dispose();
           }


       }


NOW... How can I redraw the panel to move the images to the empty spaces left by the deleted ones?
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 28-Jul-13 1:19am    
This is not a solution, does not belong here.
—SA
First of all, you cannot dispose PictureBox, just because this class does not implement the interface System.IDisposable.

Much worse, you don't understand the concept of "deletion" and the whole idea of a managed system. In such systems, you don't release memory or dispose any managed memory (dispose does exist but is used for some very different purposes). Instead, you just stop using the object, and the Garbage Collector will reclaim its memory (after calling its destructor, by the way) some time later, when the object will be detected as unreachable. This is well explained here:
http://en.wikipedia.org/wiki/Garbage_collection_%28computer_science%29[^].

Now, stop using some object and its desctruction are related but independent things. You need to exclude this object from UI, that's all you need. Even if you could destroy the object, it would be utterly dangerous is IU (or anything else) is using it; it's so good that you cannot to it in CLR. In this case, to remove object, you need to use System.Windows.Forms.Controls.Remove on the parent control:
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.controls.aspx[^].

That's not all. I cannot be sure that you really need to use this class at all, because CodeProject practice shows that his class is one of those abused by the beginners the most. I just thought you would need to be warned, even if your use is reasonable. Please see my past answers:
Append a picture within picturebox[^],
draw a rectangle in C#[^],
How do I clear a panel from old drawing[^],
capture the drawing on a panel[^],
What kind of playful method is Paint? (DataGridViewImageCell.Paint(...))[^],
Drawing Lines between mdi child forms[^].

—SA
 
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