Click here to Skip to main content
13,145,068 members (56,310 online)
Rate this:
 
Please Sign up or sign in to vote.
See more:
Hi everyone,

Is there a way to simplify this code. instead of enableing and disabling the pictureboxs
pictureBox1.MouseEnter += form_MouseClick;
pictureBox1.MouseEnter += form_MouseClick;

Please see the code.

What I have tried:

private void form_MouseClick(object sender, EventArgs e)
       {
           PictureBox picturebox = sender as PictureBox;
           SaveFileDialog savefile = new SaveFileDialog();
           savefile.Filter = @"Images|*.png";
           if (savefile.ShowDialog() == DialogResult.OK)
           {
               string filepath = Path.GetExtension(savefile.FileName);
               if (picturebox != null && filepath != null)
               {
                   Image image = picturebox.Image;
                   SaveImage(image, savefile.FileName);
               }
           }
           disable_clickevent();
       }
       private static void SaveImage(Image image, string destPath)
       {
           image.Save(destPath, System.Drawing.Imaging.ImageFormat.Png);
       }
       private void savaImageToolStripMenuItem_Click(object sender, EventArgs e)
       {
           pictureBox1.MouseEnter += form_MouseClick;
           pictureBox2.MouseEnter += form_MouseClick;
       }
       private void disable_clickevent()
       {
           pictureBox1.MouseEnter -= form_MouseClick;
           pictureBox2.MouseEnter -= form_MouseClick;
       }
Posted 21-Apr-17 4:09am
Updated 21-Apr-17 18:25pm
Comments
gggustafson 21-Apr-17 10:46am
   
Suggestion: you have two separate events - that suggests that you have two separate event handlers, one for MouseClick and one for MouseEnter. Remember "divide and conquer"? If processing is going to be the same, then call a common method from both handlers.
Member 12277263 21-Apr-17 10:48am
   
Thanks,
but is there way by using ContextMenuStrip.SourceControl to get the PictureBox without adding/removing these events.
Ralf Meier 22-Apr-17 9:26am
   
I don't understand what you try to achieve - sorry.
Perhaps you give a better explaining ...
In my opinion you need not remove the MouseEnter.Events or assign it's handler to the form ...
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Ah well. One problem you have there is that the subscription method can get called repeatedly meaning that the event handler could end up getting called more than once, which is pretty grim.

Also, this seems to go against the way things normally happen, you don't click on a menu item to enter a mode which then configures events to do something.

The event is there to let you know an item has been clicked, not to invoke a more sophisticated action. Could you not use a right-click context menu on the picture boxes instead?
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

Click this - i think it will help better

Control.MouseEnter Event (System.Windows.Forms)[^]
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy |
Web01 | 2.8.170915.1 | Last Updated 22 Apr 2017
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100