gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 17:10:49 24 4
gpt4 key购买 nike

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

enter image description here

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

关于c# - NumericUpDown 控件应仅在滚动完成时触发 ValueChanged 事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11774243/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com