在运行时删除Picturebox .net C# [英] Delete a Picturebox in runtime .net C#

查看:109
本文介绍了在运行时删除Picturebox .net C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我在C#中有一个Picturebox数组,如果用户按下del键,如何删除图片框?



这里有多远:



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


}





现在,我如何获得i值(这是阵列中选定的图像吗?



当我创建图片盒阵列时,我这样做:

 picbox [i] .Tag = i; 





我试过这个:



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



但是我得到了这个:

无法将'WindowsFormsApplication1.Principal'类型的对象强制转换为'System.Windows.Forms.PictureBox'。



谢谢!

解决方案

好的,所以我通过声明一个变量(public)然后在click事件中解决了它picbox:



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

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


}





然后:

 私有  void  Principal_KeyDown( object  sender,KeyEventArgs e)
{
if (e.KeyCode == Keys.Delete)
{

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


}





现在......我怎样才能重绘面板将图像移动到被删除的空白留下的空白区域?


首先,你不能处理 PictureBox ,只是因为这个类没有实现接口 System.IDisposable



更糟糕的是,你不明白这个概念删除和管理系统的整体思想。在此类系统中,您不释放内存或处置任何托管内存(dispose确实存在但用于某些非常不同的目的)。相反,你只是停止使用该对象,并且垃圾收集器将在一段时间之后回收其内存(在调用其析构函数之后),此时对象将被检测为无法访问的。这里有很好的解释:

http://en.wikipedia.org/wiki/ Garbage_collection_%28computer_science%29 [ ^ ]。



现在,停止使用某个对象,它的解构是相关但独立的事情。您需要从UI中排除此对象,这就是您所需要的一切。即使你可以摧毁这个物体,IU(或其他任何东西)使用它也是完全危险的;它是如此之好,你不能在CLR中。在这种情况下,要删除对象,您需要在父控件上使用 System.Windows.Forms.Controls.Remove

http://msdn.microsoft.com/en-us/library/system.windows .forms.control.controls.aspx [ ^ ]。



这不是全部。我不能确定你真的需要使用这个类,因为CodeProject实践表明他的类是最初被初学者滥用的类之一。我只是觉得你需要警告,即使你的使用是合理的。请查看我过去的答案:

在其中附加图片图片框 [ ^ ],

画一个矩形C# [ ^ ],

如何从旧图纸中清除面板 [ ^ ],

在面板上捕获绘图 [ ^ ],

什么有点俏皮的方法是Paint? (DataGridViewImageCell.Paint(...)) [ ^ ],

mdi子表单之间的绘制线 [ ^ ]。



-SA

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:

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:

picbox[i].Tag= i;



I tried this:

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!

解决方案

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

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

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

      }



then:

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?


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


这篇关于在运行时删除Picturebox .net C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆