询问NumericUpDown控制技巧 [英] Asking NumericUpDown control tricks

查看:55
本文介绍了询问NumericUpDown控制技巧的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好;



我在Windows窗体上使用NumericUpDown控件,执行时按Delete键或Backspace键使字段为空。



如果出现这种情况,我该如何将值重置为最小值?

谢谢

Hello;

I'm using NumericUpDown control on a windows form, when executing, pressing Delete key or Backspace key make the field empty.

how can i reset the value to its minimum value when this situation occurs ?
thank you

推荐答案

控件实际上已经表现得非常合理。您缺少一个重要细节:当您在控件上键入任何内容时,其属性 Value 实际上未被修改。过滤掉非数字,但您可以使用Del或Backspace,实际上将文本输入设为空。您缺少的另一个细节是:同样,您实际上可以输入太多数字来键入高于最大值值的数字。它甚至可以是无法解析为值数值的字符串。不过,没有任何不好的事情发生:价值尚未修改。



当你按回车键或控制失去焦点。那时,控件中会写入一些有效值,并且相应地修改属性 Value



I尝试考虑修改文本而在控件中编辑它(主要是对于 TextBox )并得出结论:过滤掉一些输入完全正常,但任何对输入代码的修改是邪恶的。它只能混淆用户(甚至可能是你自己)。如果有一种方法可以在控件中键入错误的字符串,则应允许用户这样做。 不应将键入数据视为输入数据。在用户执行指示输入结束和实际使用数据之前的操作之后,应确定(并在视觉上更新)实际数值。 NumericUpDown 的情况如何。



-SA
The control actually already behaves pretty reasonably. You are missing one important detail: when you type anything on the control, its property Value is not actually modified. Non-digits are filtered out, but you can use Del or Backspace and actually make the text input empty. Another detail you are missing is: likewise, you can actually type too many digits to type the number above the value of the Maximum. It can be even the string which would fail to parse as a value numeric value. Still, nothing bad happens: Value is not yet modified.

It is actually modified when you press Enter or the control looses focus. At that time, some valid value is written in the control and the property Value is modified accordingly.

I tried to think about modification of the text while one edits it in a control (mostly, for TextBox) and came to the conclusion: filtering out some input is perfectly fine, but any modifications of the entering code is evil. It can only confuse the user (and maybe even yourself). If there is a way to type wrong string in a control, the user should be allowed to do so. Typing data should not be considered as entering data. The actual numeric value should be determined (and visually updated) later, after the user do something indicating the end of input and before the actual use of the data. Which is the case for NumericUpDown.

—SA


如果你的目标是确保如果value-entry-field为空,NumericUpDown Control重置为最小值,并且NumericUpDown Control失去Focus:
If your goal is to make sure the NumericUpDown Control is reset to its minimum value if the value-entry-field is empty, and the NumericUpDown Control loses Focus:
private void numericUpDown1_Leave(object sender, EventArgs e)
{
    if (numericUpDown1.Text == String.Empty)
    {
        numericUpDown1.Value = numericUpDown1.Minimum;
        numericUpDown1.Text = numericUpDown1.Value.ToString();
    }
}

处理退格和删除,并检测值输入字段为空时可以完成:

Handling backspace and delete, and detecting when the value-entry field is empty can be done:

private void numericUpDown1_KeyUp(object sender, KeyEventArgs e)
{
    if (numericUpDown1.Text == String.Empty)
    {
        numericUpDown1.Value = numericUpDown1.Minimum;
        numericUpDown1.Text = numericUpDown1.Value.ToString();
    }
}

然而,使用第二个代码示例会产生一种尴尬的行为:当重置最小值并重置其Text表示时,光标将坚持要去到编辑字段中的起始位置(无法移动我在C#中知道的那个光标)。如果我想编辑超出数字的第一位数字,我必须手动移动光标位置。



并且,假设您将最小值设置为#40,然后你想编辑它并将其改为#80;如果您正在使用第二个代码示例,如果清除值输入字段,它将再次重置为#40,因此您必须通过移动插入光标并选择/或删除第一个数字来做一点跳舞只有。



根据人的不同,鉴于您可以使用箭头键移动插入点,您使用代码示例2支付的这个价格可能没问题或者不行。



与许多MS提供的控件一样,NumericUpDown控件有其怪癖和限制。要求......正如你在上面的两个代码示例中所看到的......在我们在代码中成功设置Value之后必须在value-entry-field中重新绘制Text是相当愚蠢的(并且没有我发现可以删除该要求的更新/刷新/无效组合。)

However, using the second code example creates an "awkward" behavior: when the minimum value is reset, and its Text representation reset, the cursor will insist on going to the start-position in the edit field (no way to move that cursor that I know of in C#). If I want to edit "beyond" first digit of the number, I've got to manually shift the cursor position.

And, assume you set the minimum value to #40, and then you want to edit that and change it to #80; if you are using the second code example, if you clear the value-entry-field, it will reset to #40 again, so you've got to do a little dance with moving the insertion cursor and selecting/or deleting the first digit only.

Depending on the person, given you can use the arrow-keys to move the insertion point, this "price" you pay with code example 2 may be okay, or not okay.

The NumericUpDown Control, like many of the MS supplied Controls, has its quirks, and limits. The requirement ... as you can see in both code examples above ... to have to re-draw the Text in the value-entry-field after we've successfully set the Value in code is rather stupid (and there is no combination of Update/Refresh/Invalidate that I've found that can remove that requirement).


感谢您的回答,比尔,谢尔盖;

我'我正在尝试你的建议和解决方案,并会尽快发表评论。



由于我将多次使用NumericUpDown控件,我甚至会考虑从原始控件中继续执行任何操作最终解决方案。

谢谢
Thank you for your answers , Bill, Sergey;
I'm trying your advices and solutions and will comment that soon.

As i will use the NumericUpDown control many times, i will even consider iheriting from the raw one to implement any eventual solution.
Thank you


这篇关于询问NumericUpDown控制技巧的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆