3

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

在此处输入图像描述

我想听听这个ValueChanged事件。

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

但:

我希望我可以向上或向下“滚动”。(如果我让它更长,它会更快)

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

我怎样才能做到这一点?

4

4 回答 4

1

尝试使用 mouseup 事件。当您将手指从鼠标左键上移开时它会触发,因此理论上它应该可以解决您的问题。

[由 James 编辑] 在您的表单上尝试此控件。

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()); }
            }
        }
    }
}
于 2012-08-02T09:14:20.187 回答
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. 这段代码并不完美。

于 2012-08-02T09:18:13.110 回答
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
        }
    }
于 2014-10-08T23:44:55.593 回答
0

这是我解决这个问题的方法:

任务检查“valueIsChanging”标志。此标志由 ValueChanged 事件设置。在该标志停止由 ValueChanged 事件设置为 True 后,while 循环完成并执行您的代码。

Task waitForValueSettleTask;
volatile bool valueIsChanging; // marked volaltile since it is access by multiple threads (the GUI thread, and the task)

private void numericUpDown_ValueChanged(object sender, EventArgs e)
{
    valueIsChanging = true;

    if (waitForValueSettleTask == null || waitForValueSettleTask != null &&
    (waitForValueSettleTask.IsCanceled || waitForValueSettleTask.IsCompleted || waitForValueSettleTask.IsFaulted))
    {
        waitForValueSettleTask = Task.Run(() =>
        {
            while (valueIsChanging)
            {
                valueIsChanging = false;
                Thread.Sleep(1000); // Set this to whatever settling time you'd like
            }

            // Your code goes here
        });
    }
}
于 2017-06-27T18:38:52.287 回答