Delete Image from PictureBox in C#
Asked Answered
A

4

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 = "";

           }
Argentic answered 29/12, 2010 at 11:16 Comment(0)
I
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;
}
Indention answered 29/12, 2010 at 11:27 Comment(8)
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.Indemnity
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.Unbated
@Jorge:I have 4 pic box in my Form,is this handles the deletion of selected image or I need to handle it explicitly?Argentic
@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 ;)Indention
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?Indemnity
@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.Indention
@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.Indemnity
@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...sorryArgentic
I
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.

Indemnity answered 29/12, 2010 at 11:24 Comment(0)
U
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;
  }
}
Unbated answered 29/12, 2010 at 11:30 Comment(0)
D
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

Delate answered 25/1, 2016 at 19:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.