NumericUpDown控件应仅在滚动完成时触发ValueChanged事件

时间:2012-08-02 09:03:21

标签: c# winforms events numericupdown

我有一个numericUpDown控件(Windows窗体)。

enter image description here

我想听一下ValueChanged事件。

我在属性中设置它并且它有效。

可是:

我希望我可以向上或向下“滚动”。 (如果我把它做得更久会更快)

当我完成“滚动”时,我希望现在触发事件xyz而不是在滚动期间。

我该怎么做?

4 个答案:

答案 0 :(得分:1)

尝试使用mouseup事件。当你用手指离开鼠标左键时会触发它,所以理论上它应该可以解决你的问题。

[詹姆斯编辑] 在表单上尝试此控件。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Example.CustomControl
{
    /// <summary>
    /// Provides an extra event for the numericUpDown control that fires after the value stops scrolling.
    /// </summary>
    public class NumericDelayedChange : NumericUpDown
    {
        /// <summary>
        /// Flag that the value has actually changed.
        /// </summary>
        /// <devdoc>
        /// Just in case the control was clicked somewhere other than the up/down buttons.
        /// </devdoc>
        private bool valueHasChanged = false;

        /// <summary>
        /// Fires when the value has stopped being scrolled.
        /// </summary>
        public event EventHandler OnAfterScollValueChanged;

        /// <summary>
        /// Captures that value as having changed.
        /// </summary>
        /// <param name="e"></param>
        protected override void OnValueChanged(EventArgs e)
        {
            valueHasChanged = true;
            base.OnValueChanged(e);
        }

        /// <summary>
        /// Captures the mouse up event to identify scrolling stopped when used in combination with the value changed flag.
        /// </summary>
        /// <param name="mevent"></param>
        protected override void OnMouseUp(MouseEventArgs mevent)
        {
            base.OnMouseUp(mevent);
            if (mevent.Button == System.Windows.Forms.MouseButtons.Left)
            {
                PerformOnAfterScollValueChanged();
            }
        }

        /// <summary>
        /// Captures the key up/down events to identify scrolling stopped when used in combination with the value changed flag.
        /// </summary>
        /// <param name="mevent"></param>
        protected override void OnKeyUp(KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Up || e.KeyCode == Keys.Down)
            {
                PerformOnAfterScollValueChanged();
            }
            base.OnKeyUp(e);
        }

        /// <summary>
        /// Checks the value changed flag and fires the OnAfterScollValueChanged event.
        /// </summary>
        private void PerformOnAfterScollValueChanged()
        {
            if (valueHasChanged)
            {
                valueHasChanged = false;
                if (OnAfterScollValueChanged != null) { OnAfterScollValueChanged(this, new EventArgs()); }
            }
        }
    }
}

答案 1 :(得分:1)

您需要定义I'm done with the scroll。这可能是从滚动按钮移除鼠标,或从最后一次点击后经过一定时间。在任何情况下,我认为你不能导致事件不运行,但你可以在事件处理程序中做一些检查。例如:

private DateTime? lastClickTime;
public void MyUpDown_ValueChanged(object sender, EventArgs e)
{
    if (lastClickTime != null && DateTime.Now.Subtract(lastClickTime.Value).Seconds > someInterval)
    {
        // Do the work
    }

    lastClickTime = DateTime.Now
}

但是,正如我所说,你首先需要定义什么是I'm done。这段代码并不完美。

答案 2 :(得分:0)

以下是解决问题的直接方法: 只需检查是否正在使用Control.MouseButtons按下鼠标按钮,如下所示:

private void numericUpDown1_ValueChanged(object sender, EventArgs e)
    {
        //don't update until done scrolling
        if (Control.MouseButtons == MouseButtons.None)
        {
            // Do the work
        }
    }

答案 3 :(得分:0)

以下是我如何解决这个问题:

任务检查&#34; valueIsChanging&#34;旗。此标志由ValueChanged事件设置。通过ValueChanged事件将此标志设置为True后,while循环完成并执行您的代码。

   background-size:100% 100%;
    background-attachment:fixed;