Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在C中从PictureBox中删除图像#_C#_.net_Winforms_Picturebox - Fatal编程技术网

C# 在C中从PictureBox中删除图像#

C# 在C中从PictureBox中删除图像#,c#,.net,winforms,picturebox,C#,.net,Winforms,Picturebox,如何在用户按“删除”键时从图片框中删除图像…我找不到PB的任何按键或按键事件 private void topRight_pbx_MouseClick(object sender, MouseEventArgs e) { imgSelected=true; //need to accept "delete"key from keyboard? topRight_pbx.Image = null;

如何在用户按“删除”键时从图片框中删除图像…我找不到PB的任何按键或按键事件

    private void topRight_pbx_MouseClick(object sender, MouseEventArgs e)
          {
           imgSelected=true;

           //need to accept "delete"key from keyboard?

           topRight_pbx.Image = null;
            topRFile = "";

           }

这是因为
PictureBox
控件永远无法获得焦点,而非焦点控件不接收键盘输入事件。

    private void topRight_pbx_MouseClick(object sender, MouseEventArgs e)
          {
           imgSelected=true;

           //need to accept "delete"key from keyboard?

           topRight_pbx.Image = null;
            topRFile = "";

           }
如图所示,
KeyDown
事件(以及与键盘输入相关的其他事件)被标记为
[BrowsableAttribute(false)]
,因为它们不能按预期工作。您的代码不打算订阅它们

    private void topRight_pbx_MouseClick(object sender, MouseEventArgs e)
          {
           imgSelected=true;

           //need to accept "delete"key from keyboard?

           topRight_pbx.Image = null;
            topRFile = "";

           }
它类似于
标签
控件,您可以查看它,但它不可选择,也无法获得焦点

    private void topRight_pbx_MouseClick(object sender, MouseEventArgs e)
          {
           imgSelected=true;

           //need to accept "delete"key from keyboard?

           topRight_pbx.Image = null;
            topRFile = "";

           }

您需要为用户找到另一种方式来表示他想要删除当前显示在
PictureBox
控件中的图像。

将您的imgSelected更改为类似以下内容:

    private void topRight_pbx_MouseClick(object sender, MouseEventArgs e)
          {
           imgSelected=true;

           //need to accept "delete"key from keyboard?

           topRight_pbx.Image = null;
            topRFile = "";

           }
private PictureBox picSelected = null;
在picturebox上,单击将此变量设置为发件人:

    private void topRight_pbx_MouseClick(object sender, MouseEventArgs e)
          {
           imgSelected=true;

           //need to accept "delete"key from keyboard?

           topRight_pbx.Image = null;
            topRFile = "";

           }
picSelected = (PictureBox)sender;
然后在窗体或具有焦点的控件的键下,运行图像删除代码 (表格示例):

    private void topRight_pbx_MouseClick(object sender, MouseEventArgs e)
          {
           imgSelected=true;

           //need to accept "delete"key from keyboard?

           topRight_pbx.Image = null;
            topRFile = "";

           }

我在我的一个项目中遇到了类似的问题。我通过添加一个屏幕外文本框解决了这个问题。单击某些控件时,我会将焦点放在文本框上,并使用文本框处理键盘输入

    private void topRight_pbx_MouseClick(object sender, MouseEventArgs e)
          {
           imgSelected=true;

           //need to accept "delete"key from keyboard?

           topRight_pbx.Image = null;
            topRFile = "";

           }
PicureBox SelectedImage=null;

void Image_Click(object sender,...)
{
  SelectedImage=(PictureBox)sender;
  FocusProxy.Focus();
}

void FocusProxy_KeyDown(...)
{
  if(e.KeyData==...)
  {
       SelectedImage.Image=null;
       e.Handled=true;
  }
}

另一种方法是: 如果要在pictureBox上绘图,并且要清除它,请执行以下操作:

    private void topRight_pbx_MouseClick(object sender, MouseEventArgs e)
          {
           imgSelected=true;

           //need to accept "delete"key from keyboard?

           topRight_pbx.Image = null;
            topRFile = "";

           }
Graphics g = Graphics.FromImage(this.pictureBox1.Image);
g.Clear(this.pictureBox1.BackColor);
之后,可以再次在控件上绘制

    private void topRight_pbx_MouseClick(object sender, MouseEventArgs e)
          {
           imgSelected=true;

           //need to accept "delete"key from keyboard?

           topRight_pbx.Image = null;
            topRFile = "";

           }

当然,如果你有多个
PictureBox
控件,你仍然需要一些外部方法来指示选择了哪一个,以免用户无意中删除了错误的图像。我忘记了细节,但这种方法有一些问题。我认为这是因为在某些情况下表单没有获得关键事件。当然,你只想在选中图像时删除图像,而不是用户只想删除字符的其他文本控件。@Jorge:我的表单中有4个pic框,这是处理所选图像的删除还是我需要显式处理?@Sisya:如果你将相同的代码放在所有4个pic框中单击(通过转到同一个单击处理程序或复制粘贴)它将适用于所有这些操作,只删除用户最后单击的一个。@CodeInChaos:添加代码以聚焦带有keydown事件的隐藏文本框可以解决此问题;)我不明白任何人如何期望它在多个PictureBox中正常工作,或者为什么这是公认的答案。问题中为
MouseClick
事件提供的代码已删除图像。显然,关键是要用键盘。但是,当用户按下键盘上的Del键时,您将如何检测用户试图在哪个
PictureBox
中删除图像?
    private void topRight_pbx_MouseClick(object sender, MouseEventArgs e)
          {
           imgSelected=true;

           //need to accept "delete"key from keyboard?

           topRight_pbx.Image = null;
            topRFile = "";

           }