Click here to Skip to main content
15,884,978 members
Please Sign up or sign in to vote.
1.67/5 (3 votes)
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
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 ...

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?
 
Share this answer
 
Click this - i think it will help better

Control.MouseEnter Event (System.Windows.Forms)[^]
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900