簡體   English   中英

NumericUpDown控件應僅在滾動完成時觸發ValueChanged事件

[英]NumericUpDown Control should fire ValueChanged Event only on scroll finished

我有一個numericUpDown控件(Windows窗體)。

在此輸入圖像描述

我想聽ValueChanged事件。

我在屬性中設置它,它的工作原理。

但:

我希望我可以“向上或向下滾動”。 (如果我把它做得更久會更快)

當我完成“滾動”時,我希望事件xyz現在被激活而不是在滾動期間。

我怎樣才能做到這一點?

嘗試使用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()); }
            }
        }
    }
}

你需要定義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 這段代碼並不完美。

這是解決問題的直接方法:只需檢查是否使用Control.MouseButtons按下鼠標按鈕,如下所示:

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

這是我解決這個問題的方法:

任務檢查“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
        });
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM