如何删除鼠标输入事件 [英] How to remove the mouse enter event

查看:81
本文介绍了如何删除鼠标输入事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



有没有办法简化这段代码。而不是启用和禁用图片框

pictureBox1.MouseEnter + = form_MouseClick;

pictureBox1.MouseEnter + = form_MouseClick;



请参阅代码。



我尝试过:



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;
       }

推荐答案

好吧。你遇到的一个问题是,可以重复调用订阅方法,这意味着事件处理程序可能最终被多次调用,这是非常严峻的。



另外,这似乎违反了通常发生的事情,你没有点击菜单项进入一种模式,然后配置事件来做某事。



event用于通知您已单击某个项目,而不是调用更复杂的操作。你不能在图片框上使用右键单击上下文菜单吗?
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?


点击这个 - 我认为它会有所帮助



Control.MouseEnter事件(系统。 Windows.Forms) [ ^ ]
Click this - i think it will help better

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


这篇关于如何删除鼠标输入事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆