7

how to delete image from picture box when user press "del" key...I dont find any keypress or keydown 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 = "";

           }
Dark Knight
  • 3,507
  • 8
  • 35
  • 44

4 Answers4

4

Change your imgSelected to something like:

private PictureBox picSelected = null;

On your picturebox click set this variable to the sender:

picSelected = (PictureBox)sender;

Then on the keydown of form or the control that has focus you run the image removal code (Example for form):

private void Form1_KeyDown(object sender, KeyEventArgs e)
{
   if (e.KeyCode == Keys.Delete)
      picSelected.Image = null;
}
jpiolho
  • 1,192
  • 11
  • 12
  • Of course, if you have multiple `PictureBox` controls, you'll still need some external way to indicate *which* particular one is selected, lest the user inadvertently delete the wrong image. – Cody Gray - on strike Dec 29 '10 at 11:30
  • I forgot the details but I had some problems with this approach. I think it was that the form didn't get the key events in some cases. And of course you only want to delete the image if an image is selected and not some other text control where the user just wants to delete a character. – CodesInChaos Dec 29 '10 at 11:36
  • @Jorge:I have 4 pic box in my Form,is this handles the deletion of selected image or I need to handle it explicitly? – Dark Knight Dec 29 '10 at 11:36
  • @Sisya: If you put the same code for click in all 4 of them (either by going to same click handler or copy-paste) it will work for all of them, deleting only the one last clicked by the user. @CodeInChaos: Adding code to focus a hidden textbox with the keydown event could fix that ;) – jpiolho Dec 29 '10 at 11:41
  • I don't see how anyone expects this to work properly with multiple pictureboxes, or why it's the accepted answer. The code provided in the question for the `MouseClick` event already deletes the image. Obviously, the point is to do it with the keyboard. But how are you going to detect *which* `PictureBox` the user is attempting to delete the image in when they press the Del key on their keyboard? – Cody Gray - on strike Dec 29 '10 at 11:44
  • @Cody Gray: It's stored in picSelected variable, which is in form scope. When user clicks on the picturebox the variable is set to the sender, which is the picturebox itself. – jpiolho Dec 29 '10 at 11:45
  • @Jorge: Yeah, so your solution requires that the user *clicks* on the `PictureBox` first, before they can use the delete key. Except that in the code posted in the question, the `MouseClick` event already removes the image from the `PictureBox`. This seems entirely redundant. – Cody Gray - on strike Dec 29 '10 at 11:46
  • @Cody:actually MouseClick event to select the pic...then delete selected pic...I wanted to put it in keydown event...but dint know how to handle it so put it in MouseClick to test...sorry – Dark Knight Dec 29 '10 at 11:55
2

That's because the PictureBox control can never get the focus, and non-focused controls do not receive keyboard input events.

As the documentation shows, the KeyDown event (and the other events related to keyboard input) are marked with [BrowsableAttribute(false)] because they do not work as expected. They are not intended to be subscribed to by your code.

It's similar to a Label control—you can look at it, but it's not selectable and can't acquire the focus.

You'll need to find another way for the user to indicate that (s)he wants to delete an image currently displayed in a PictureBox control.

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
1

A different way for this could be: If you are drawing on a pictureBox and you want to clear it:

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

After that you can draw again on the control.

I hope this can help to someone

Orlando Herrera
  • 3,481
  • 1
  • 34
  • 44
1

I had a similar problem in one of my projects. I solved it by adding an off-screen textbox. I give focus to the text-box when certain controls get clicked, and use the text-box to handle the keyboard input.

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;
  }
}
CodesInChaos
  • 106,488
  • 23
  • 218
  • 262