简体   繁体   English

如何在.NET中实时捕获numericUpDown ctrl的值更改?

[英]How to capture the value change of numericUpDown ctrl real-time in .NET?

I have implement the ValueChanged event, but I found it will be triggered only after change the focus from numericUpDown to other control or by clicking the up/down arrow. 我已经实现了ValueChanged事件,但是我发现只有在将焦点从numericUpDown更改为其他控件或单击向上/向下箭头后,才会触发该事件。

Inputting the value in the control will not trigger ValueChanged event. 在控件中输入值将不会触发ValueChanged事件。

What I did now is adding an KeyDown event and judge if the input value is numeric (can not include SHIFT/CTRL/ALT key); 我现在要做的是添加一个KeyDown事件,并判断输入值是否为数字(不能包含SHIFT / CTRL / ALT键); but there is another case need to be involved: the user can paste value into the numericUpDown ctrl 但是还需要涉及另一种情况:用户可以将值粘贴到numericUpDown ctrl中

What I need is : 我需要的是:

  1. When I was typing numeric value (only numeric) in the numericUpDown ctrl, it will known the content has been changed; 当我在numericUpDown ctrl中输入数字值(仅数字)时,它将知道内容已更改;
  2. When pasted a numeric value into the ctrl, it also kowns the content changed 将数字值粘贴到ctrl中时,它也知道更改的内容

I answered your related other question about numeric updown control: 我回答了您有关数字上下控制的其他问题:

how to hold the invalid value for NumericUpDown after it loses focus? 失去焦点后如何保存NumericUpDown的无效值?

You can use the same technique I described there to handle the TextChanged event of the embedded textbox. 您可以使用此处所述的相同技术来处理嵌入式文本框的TextChanged事件。

Best regards... 最好的祝福...

I would suggest subclassing the NumericUpDown and overriding the OnTextBoxTextChanged method to raise the ValueChanged event. 我建议子类化NumericUpDown并重写OnTextBoxTextChanged方法来引发ValueChanged事件。

You would also need to consider what represents a change, you may get unexpected results if you raise the Valuechanged when the user has typed a single digit and then a second digit. 您还需要考虑什么代表更改,如果在用户键入一个数字然后输入第二个数字时提高Valuechanged值,则可能会得到意外的结果。 Or a non-numeric character. 或非数字字符。

CodeProject to the rescue: CodeProject解救:

A Derived NumericUpDown that Provides Handlers for NumericUpDown's Up and Down Button 派生的NumericUpDown,为NumericUpDown的向上和向下按钮提供处理程序

Extended NumericUpDown control 扩展的NumericUpDown控件

And as a bonus, here's a wrapping NumericUpDown: 另外,下面是包装的NumericUpDown:

class WrappingNumericUpDown : NumericUpDown
{
    public override void DownButton()
    {
        if (this.Value == this.Minimum)
            this.Value = this.Maximum;
        else
            base.DownButton();
    }

    public override void UpButton()
    {
        if (this.Value == this.Maximum)
            this.Value = this.Minimum;
        else
            base.UpButton();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM