Delete image from PictureBox in C #

how to remove an image from the image window when the user presses the "del" key ... I do not find any keypress or key events for PB.

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

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

           topRight_pbx.Image = null;
            topRFile = "";

           }
+5
source share
4 answers

Change your imgSelected to something like:

private PictureBox picSelected = null;

On your sample, click this variable for the sender:

picSelected = (PictureBox)sender;

Then, when you click the form button or control with focus, you run the image deletion code (Example for the form):

private void Form1_KeyDown(object sender, KeyEventArgs e)
{
   if (e.KeyCode == Keys.Delete)
      picSelected.Image = null;
}
+3
source

This is because the control can PictureBoxnever get focus, and non-focused controls do not receive keyboard input events.

, KeyDown ( , ) [BrowsableAttribute(false)], . .

Label - , .

, () , PictureBox.

+2

. , . , .

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;
  }
}
+1

: PictureBox :

Graphics g = Graphics.FromImage(this.pictureBox1.Image);
g.Clear(this.pictureBox1.BackColor);

.

Hope this helps someone.

0
source

All Articles