首页 新闻 会员 周边 捐助

怎么防止在执行双击事件前触发单击事件,谢谢

0
悬赏园豆:10 [已解决问题] 解决于 2010-09-26 09:51

比如Winform控件ListView,我想单击Item时执行一个操作,双击Item时执行另一个操作,可是双击Item时总是先出发了单击事件,不知道大家有何良策。

问题补充: 打错个字,,是“触发”,不是“出发”
会长的主页 会长 | 专家六级 | 园豆:12461
提问于:2010-09-25 16:47
< >
分享
最佳答案
0

我这里有一个鼠标类,感觉可以处理你这个问题。

public class MouseClickManager
    {
        public event MouseButtonEventHandler Click;
        public event MouseButtonEventHandler DoubleClick;

        /// <summary>
        /// Gets or sets a value indicating whether this <see cref="MouseClickManager"/> is clicked.
        /// </summary>
        /// <value><c>true</c> if clicked; otherwise, <c>false</c>.</value>
        private bool Clicked { get; set; }

        /// <summary>
        /// Gets or sets the control.
        /// </summary>
        /// <value>The control.</value>
        public Control Control { get; set; }

        /// <summary>
        /// Gets or sets the timeout.
        /// </summary>
        /// <value>The timeout.</value>
        public int Timeout { get; set; }

        /// <summary>
        /// Initializes a new instance of the <see cref="MouseClickManager"/> class.
        /// </summary>
        /// <param name="control">The control.</param>
        public MouseClickManager(Control control, int timeout)
        {
            this.Clicked = false;
            this.Control = control;
            this.Timeout = timeout;
        }

        /// <summary>
        /// Handles the click.
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.Windows.Input.MouseButtonEventArgs"/> instance containing the event data.</param>
        public void HandleClick(object sender, MouseButtonEventArgs e)
        {
            lock (this)
            {
                if (this.Clicked)
                {
                    this.Clicked = false;
                    OnDoubleClick(sender, e);
                }
                else
                {
                    this.Clicked = true;
                    ParameterizedThreadStart threadStart = new ParameterizedThreadStart(ResetThread);
                    Thread thread = new Thread(threadStart);
                    thread.Start(e);
                }
            }
        }

        /// <summary>
        /// Resets the thread.
        /// </summary>
        /// <param name="state">The state.</param>
        private void ResetThread(object state)
        {
            Thread.Sleep(this.Timeout);

            lock (this)
            {
                if (this.Clicked)
                {
                    this.Clicked = false;
                    OnClick(this, (MouseButtonEventArgs)state);
                }
            }
        }

        /// <summary>
        /// Called when [click].
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.Windows.Input.MouseButtonEventArgs"/> instance containing the event data.</param>
        private void OnClick(object sender, MouseButtonEventArgs e)
        {
            MouseButtonEventHandler handler = Click;

            if (handler != null)
                this.Control.Dispatcher.BeginInvoke(handler, sender, e);
        }

        /// <summary>
        /// Called when [double click].
        /// </summary>
        /// <param name="sender">The sender.</param>
        /// <param name="e">The <see cref="System.Windows.Input.MouseButtonEventArgs"/> instance containing the event data.</param>
        private void OnDoubleClick(object sender, MouseButtonEventArgs e)
        {
            MouseButtonEventHandler handler = DoubleClick;

            if (handler != null)
                handler(sender, e);
        }
    }

 

用法:

  public MyClass()
        {
            InitializeComponent();

            mcm = new MouseClickManager(yourControlInstance, 300);
            mcm.Click+=new MouseButtonEventHandler(mcm_Click);
            mcm.DoubleClick+=new MouseButtonEventHandler(mcm_DoubleClick);
        }

  void  mcm_DoubleClick(object sender, MouseButtonEventArgs e)
  {
    throw new NotImplementedException();
  }

  void  mcm_Click(object sender, MouseButtonEventArgs e)
  {
    throw new NotImplementedException();
  }
        private void Button1_Click(object sender, MouseButtonEventArgs e)
        {
            mcm.HandleClick(sender, e);
        }

我这是silverlight中的,不知道在winform中可以用不,你试试吧,祝你好运!

收获园豆:10
小闵 | 小虾三级 |园豆:502 | 2010-09-25 21:49
其他回答(2)
1

让单击事件直接return false;

路过秋天 | 园豆:4787 (老鸟四级) | 2010-09-26 08:56
多谢小闵。该问题已完美解决:http://msdn.microsoft.com/en-us/library/ms171543(VS.80).aspx,这是msdn的官方解决方法。 路过秋天:不能直接return,我需要在单击时执行一些操作
支持(0) 反对(0) 会长 | 园豆:12461 (专家六级) | 2010-09-26 09:51

比如单击时弹出A窗口。双击时弹出B窗口。通常两个事件都加上的话,双击时两个窗口都弹出了。当然也不能在单击是返回,这样的话单击时不能弹出窗体了。后来找到而来解决方法。判断两次鼠标点击的事件间隔。第一次点击鼠标时开始计时,如果到一定事件还没有点击第二次,则认为是单击;反之是双击。

支持(0) 反对(0) 会长 | 园豆:12461 (专家六级) | 2011-11-22 17:52
0

楼上的是正确的

.NET快速开发框架 | 园豆:946 (小虾三级) | 2010-09-26 10:43
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册