9 Replies - 836 Views - Last Post: 19 February 2013 - 01:25 AM Rate Topic: -----

#1 varunit  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 39
  • Joined: 03-September 09

Removing a dynamically generated picture box from the form

Posted 18 February 2013 - 07:36 AM

Hi there!
I'm developing a Windows forms application which captures screenshots and create documents out of them.
What my form design looks like is as the snapshots are captured, a picture box with a thumbnail size will be created and added to the form dynamically. Whenever this picture box is clicked, an enlarged version of the image is displayed in another picture box. I've done all of this stuff.
Here's what I've done to generate picture boxes of thumbnail sizes and when they're clicked images enlarge.

private void FillImagePanel(Bitmap imgBmp)
        {
            PictureBox tempPictureBox = new PictureBox();           
            tempPictureBox.Tag = imgBmp;
            //generates a thumbnail image of specified size
            tempPictureBox.Image = imgBmp.GetThumbnailImage(100, 100,
                                   new Image.GetThumbnailImageAbort(ThumbnailCallback),
                                   IntPtr.Zero);
            tempPictureBox.Size = new System.Drawing.Size(100, 100);
            tempPictureBox.Cursor = Cursors.Hand;
            tempPictureBox.Click += new EventHandler(this.tempPictureBox_Click);
            ImageFlowLayoutPanel.Controls.Add(tempPictureBox);
        }

//This click event will be used to display the enlarged images
        private void tempPictureBox_Click(object sender, EventArgs e)
        {
            Bitmap reSizedImage = new Bitmap((Bitmap)((PictureBox)sender).Tag, 
                                  new System.Drawing.Size(600,400));

            PreviewPictureBox.Image = reSizedImage;
            
        }



Now I would also like to add the ability to delete the images snapped. I'm having trouble in doing this task.
I've set the form's KeyPreview property to True and added the KeyDown event to handle the delete key press.
priavte void SnapperForm_KeyDown(Object sender, KeyEventArgs e)
{
  if(e.KeyCode == Keys.Delete)
  {
    //write code here to delete the thumbnail sized picture box
  }
}


There will be many picture boxes generated dynamically and added to the form. My question is how do I know which picture box the user has chosen to remove from the form? How do I wire up the above event whenever an image is selected and Delete key is pressed by user?

Thanks in advance

Is This A Good Question/Topic? 0
  • +

Replies To: Removing a dynamically generated picture box from the form

#2 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1010
  • View blog
  • Posts: 2,444
  • Joined: 04-October 09

Re: Removing a dynamically generated picture box from the form

Posted 18 February 2013 - 08:03 AM

Off the top of my head, I wouldn't use the KeyPreview but I'd attach a handler to the KeyDown event to each picture box. That way you can use the sender property to figure out which one it is. Remove the handler, remove it from the controls collection and just let it go out of scope.
Was This Post Helpful? 2
  • +
  • -

#3 varunit  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 39
  • Joined: 03-September 09

Re: Removing a dynamically generated picture box from the form

Posted 18 February 2013 - 08:50 AM

Last time I checked there is not any KeyDown event for a picturebox. Intellisense didn't show any KeyDown event for it.
Was This Post Helpful? 0
  • +
  • -

#4 Michael26  Icon User is offline

  • DIC-head, major DIC-head
  • member icon

Reputation: 355
  • View blog
  • Posts: 1,519
  • Joined: 08-April 09

Re: Removing a dynamically generated picture box from the form

Posted 18 February 2013 - 09:05 AM

Have you checked here
I would check MSDN when i'm in doubt, i don't always rely on intellisense

This post has been edited by Michael26: 18 February 2013 - 09:06 AM

Was This Post Helpful? 3
  • +
  • -

#5 varunit  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 39
  • Joined: 03-September 09

Re: Removing a dynamically generated picture box from the form

Posted 18 February 2013 - 09:56 AM

View PostMichael26, on 18 February 2013 - 09:05 AM, said:

Have you checked here
I would check MSDN when i'm in doubt, i don't always rely on intellisense


I'm a bit surprised why Intellisense didn't displayed KeyDown event for picturebox

This post has been edited by varunit: 18 February 2013 - 09:57 AM

Was This Post Helpful? 0
  • +
  • -

#6 varunit  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 39
  • Joined: 03-September 09

Re: Removing a dynamically generated picture box from the form

Posted 18 February 2013 - 10:03 AM

View PostMomerath, on 18 February 2013 - 08:03 AM, said:

Off the top of my head, I wouldn't use the KeyPreview but I'd attach a handler to the KeyDown event to each picture box. That way you can use the sender property to figure out which one it is. Remove the handler, remove it from the controls collection and just let it go out of scope.


I cannot just let the picturebox out of scope. Actually I've mentioned only part of my form's design. I've actually a flowlayoutpanel which hosts these dynamically generated pictureboxes. According to your idea, I added the following code to test the Key Press.

tempPictureBox.KeyDown += new KeyEventHandler(tempPictureBox_KeyDown);

private void tempPictureBox_KeyDown(object sender, KeyEventArgs e)
{
  if(e.KeyCode == Keys.Delete)
  {
    MessageBox.show("Delete key pressed");
  }
}


But nothing happens when I pressed the Delete Key
Was This Post Helpful? 0
  • +
  • -

#7 Michael26  Icon User is offline

  • DIC-head, major DIC-head
  • member icon

Reputation: 355
  • View blog
  • Posts: 1,519
  • Joined: 08-April 09

Re: Removing a dynamically generated picture box from the form

Posted 18 February 2013 - 11:41 AM

So you are saying that message isn't shown?
Was This Post Helpful? 0
  • +
  • -

#8 varunit  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 39
  • Joined: 03-September 09

Re: Removing a dynamically generated picture box from the form

Posted 18 February 2013 - 08:16 PM

yes Michael26, message is not shown.
Was This Post Helpful? 0
  • +
  • -

#9 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1010
  • View blog
  • Posts: 2,444
  • Joined: 04-October 09

Re: Removing a dynamically generated picture box from the form

Posted 18 February 2013 - 10:35 PM

It was off the top of my head. Actually testing it shows that the PictureBox isn't getting keypress messages. With that in mind, I did this:

Added a PictureBox attribute to my form:
PicturBox selected = null;


Added thumbnails to pictureboxes that are 2 pixels larger than the thumbnail in each direction (so if thumbnail was 100x100, picturebox would be 104x104). Set Padding to 2,2,2,2.


Added this code to MouseClick event on the PictureBoxes:
private void pictureBox_Click(object sender, EventArgs e) {
    if (selected != null) {
        selected.BackColor = SystemColors.Control;
        selected = null;
    }

    PictureBox pb = sender as PictureBox;
    if (pb != null) {
        pb.BackColor = Color.Red;
        selected = pb;
    }
}


This puts a thin red line around the PictureBox when you click on it. Now do what you did in the original post but remove the one in the selected variable. Make sure you detach events.
Was This Post Helpful? 1
  • +
  • -

#10 varunit  Icon User is offline

  • New D.I.C Head

Reputation: 3
  • View blog
  • Posts: 39
  • Joined: 03-September 09

Re: Removing a dynamically generated picture box from the form

Posted 19 February 2013 - 01:25 AM

@Momerath
I have solved the problem by creating a PictureBox control as a class member variable, then updated it whenever a picturebox has been clicked, added the KeyDown event to picturebox and performing the delete operation when user presses the delete button. Anyways thank you all for the help.

I was about to post a new thread like how to outline/highlight the current picturebix selected with a border or something similar. But you have posted solution for that as well. Thank you Momerath
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1