NumericUpDown值无效 [英] NumericUpDown Invalid value
问题描述
我正在尝试解决NumericUpDown的问题(我将其称为Num)控件:让我们说Num有这些属性:
最小值:10
最大值:60
步骤:1
如果用户要更改值,可以通过两种方式进行操作:单击箭头(Num的右侧)或直接手动输入Num。第一种方式是OK,但大多数用户正在使用第二种方式,还有一点问题。
如果用户在例如1的时间间隔之外键入一些值,则可以继续输入5,因此最终值为51,这是在间隔。但是,如果他在值1之后停止打字,则意味着他在区间(1)之间输入值。如果他点击Num之外的某个地方,值(在时间间隔之外)将自动更改为最接近的允许值(如果为1,则值将更改为10)。
但他可能不会注意到这个自动更改,所以我想以某种方式处理它,并通知他,他把无效值。但是这种情况不是由任何Num的事件处理(没有办法如何找出这个无效的值,如果我尝试读取ValueChanged事件中的值,它读取自动更改的值,而不是无效的)。 / p>
我知道我可以添加 TextChanged
事件,但有一个问题,如果他键入一些无效值(5) ,可以更改为有效值(通过添加1,使其为15)。
您有什么想法可以解决这个问题吗?我知道这是愚蠢的,但这不依赖于我,我必须这样做,我不知道如何。
p>所以这是我的解决方案:
我处理 TextChanged
事件,在这个方法里面我分配文本
到其标签
:
Private Sub NUDTextChanged()
Integer.TryParse(NUD.Text,NUD.Tag)
End Sub
然后:
Private Sub NUD_LostFocus()处理NUD.LostFocus
如果NUD。标签< NUD.Minimum或NUD.Tag> NUD.Maximum Then
'show message
End If
End Sub
处理验证
事件对我来说是无用的,因为允许值的自动更改在验证之前,此自动更改会触发 TextChange
事件如此验证后,我有新的值而不是一个。
LostFocus
在自动更改之前,所以我可以轻松控制该值是否有效。
I have an application in WinForms where I have many controls. These controls represents settings of application and user can save this settings by clicking on a button.
I am trying to resolve a problem with NumericUpDown (I will call it Num) control: Lets say that Num has these properties:
Minimum: 10 Maximum: 60 Step: 1
If a user want to change value, there are two ways how to do that: clicking on arrows (right side of the Num) or manually typing value directly to Num. First way is OK but most most of users are using second way and there is a little problem.
If a user type some value out of the interval for example 1, it is OK because he could continue in typing with 5 so final value is 51 and this is inside the interval. But if he stop typing after value 1, it means he type value out of the interval (1). If he click somewhere out of the Num, value (which is out of the interval) is automatically changed to the closest allowed value (in case of 1, value will be changed to 10).
But he probably will not notice this automatic change so I want to handle it somehow and inform him that he put there invalid value. But this situation is not handled by any event of Num (there is no way how to find out this invalid value he put there - if I try to read value inside ValueChanged event, it reads automatically changed value, not the invalid one).
I know I can add TextChanged
event, but there is a problem that if he type some invalid value (5), it can be changed to valid value (by adding 1 so it makes 15).
Do you have any ideas how to resolve this issue? I know this is stupid but this doesn't depend on me, I have to do this and I don't know how.
So this is my solution:
I handle TextChanged
event and inside this method I assign text
to its tag
:
Private Sub NUDTextChanged()
Integer.TryParse(NUD.Text, NUD.Tag)
End Sub
And then:
Private Sub NUD_LostFocus() Handles NUD.LostFocus
If NUD.Tag < NUD.Minimum Or NUD.Tag > NUD.Maximum Then
' show message
End If
End Sub
Handling Validating
event is useless for me because automatic change to allowed value is before validating and this automatic change fires TextChange
event so after validating I have new value instead invalid one.
LostFocus
is before automatic change so I can easily control if the value is valid or not.
这篇关于NumericUpDown值无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!