Search code examples
c#winformseventsnumericupdown

NumericUpDown Control should fire ValueChanged Event only on scroll finished


I have a numericUpDown control (Windows Forms).

enter image description here

I want to listen to the ValueChanged event.

I set it in the properties and it works.

But:

I want that i can "scroll" up or down. (If I make this longer it will be faster)

When I'm done with the "scroll", I want that the event xyz is fired now and not during the scrolling.

How can I do that?


Solution

  • Try using the mouseup event. It fires when you take your finger off of the left mouse button, so in theory it should solve your issue.

    [Edited by James] Try this control on your form.

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