仅文本框中的数字值 [英] Only Numeric Values in the textbox
问题描述
我编写了以下代码,允许用户只输入数字和
只有一个小数点。这样可以正常工作。
但是,我希望用户不要在小数点后输入第三个数字
点,例如用户不应该被允许输入12557.234。用户应该允许
只允许在小数点后输入两位数。
我知道我可以在验证事件中验证它,但是,我想知道是否
有些人可以修改以下代码,以便在按键事件中完成。
提前致谢!
如果是Asc( e.KeyChar)<> Keys.Back然后
如果e.KeyChar.IsNumber(e.KeyChar)= False那么
如果e.KeyChar<> "。"然后
ze.Handled = True
否则
如果txtAmountFigure.Text.IndexOf("。")<> -1然后
e.Handled = True
结束如果
结束如果
结束如果
结束如果
REM txtAmountFigure = control。
Dim intLoc As Integer = txtAmountFigure .Text.LastIndexOf("。" c)
e.Handled = true
如果intLoc> = 0那么
如果。 Text.Substring(intLoc).Length< = 2然后
e.Handled = False
结束如果
否则
e.Handled = False
结束如果
" patang" < PA **** @ discussions.microsoft.com> schreef in bericht
新闻:5C ********************************** @ microsof t.com ...我编写了以下代码,允许用户只输入数字
和
只有一个小数点。这样可以正常工作。
但是,我想用户不应该在十进制数点后输入第三个数字,例如用户不应该被允许输入12557.234。用户
只允许在小数点后输入两位数。
我知道我可以在验证事件中验证它,但是,我想知道
如果
有些人可以修改以下代码,以便在按键事件中完成。
提前致谢!
如果Asc(e.KeyChar)<> Keys.Back然后
如果e.KeyChar.IsNumber(e.KeyChar)= False那么
如果e.KeyChar<> "。"然后
ze.Handled = True
否则
如果txtAmountFigure.Text.IndexOf("。")<> -1然后
e.Handled = True
结束如果
结束如果
结束如果
结束如果
" patang" < PA **** @ discussions.microsoft.com> schrieb:我编写了以下代码,允许用户只输入数字
和
只有一个小数点。这样可以正常工作。
但是,我想用户不应该在十进制数点后输入第三个数字,例如用户不应该被允许输入12557.234。用户
只允许在小数点后输入两位数。
我知道我可以在验证事件中验证它,但是,我想知道
如果
有些人可以修改以下代码,以便在按键事件中完成。
我不认为这是个好主意。验证
控件时应进行验证(''验证''事件),而不是在用户输入文本时进行验证。
这允许用户就地进行验证例如,编辑从
剪贴板粘贴的内容。通常,如果在文本框中输入的文本
无效,则会设置错误提供程序。
-
MS Herfried K. Wagner
MVP< URL:http://dotnet.mvps.org/>
VB< URL:http://classicvb.org/petition/> ;
把它放入KeyPress事件:
Dim KeyAscii As Integer
KeyAscii = Asc(e.KeyChar)
Select Case KeyAscii
案例48至57,8,13
案例45
如果是InStr(Me.Text," - ")<> 0然后
KeyAscii = 0
结束如果
如果Me.SelectionStart<> 0然后
KeyAscii = 0
结束如果
案例46
如果是InStr(Me.Text," 。""> 0然后
KeyAscii = 0
结束如果
Case Else
KeyAscii = 0
结束选择
如果KeyAscii = 0那么
e.Handled = True
Else
e.Handled = False
结束如果
这也接受一个小数点,一个减号。
如果Herfried想要为了对上面的代码嗤之以鼻,我将发布
代码来停止所有复制/粘贴/剪切shorcut处理程序,因为Herfried曾经提到过它之前的b $ b。虽然,他从未提出过解决方案。
I have written the following code which allows user to enter only numbers and
only one decimal point. This works fine.
However, I want that user shouldn''t enter a third number after decimal
point, for example user should not be allowed to enter 12557.234. User should
only be allowed to enter two digits after decimal point.
I know I can validate it in the validate event, however, I was wondering if
some can modify the following code so that it be done in keypress event.
Thanks in advance!
If Asc(e.KeyChar) <> Keys.Back Then
If e.KeyChar.IsNumber(e.KeyChar) = False Then
If e.KeyChar <> "." Then
ze.Handled = True
Else
If txtAmountFigure.Text.IndexOf(".") <> -1 Then
e.Handled = True
End If
End If
End If
End If
REM txtAmountFigure = control.
Dim intLoc As Integer = txtAmountFigure.Text.LastIndexOf("."c)
e.Handled = true
If intLoc >= 0 Then
If .Text.Substring(intLoc).Length <= 2 Then
e.Handled = False
End If
Else
e.Handled = False
End If
"patang" <pa****@discussions.microsoft.com> schreef in bericht
news:5C**********************************@microsof t.com...I have written the following code which allows user to enter only numbers
and
only one decimal point. This works fine.
However, I want that user shouldn''t enter a third number after decimal
point, for example user should not be allowed to enter 12557.234. User
should
only be allowed to enter two digits after decimal point.
I know I can validate it in the validate event, however, I was wondering
if
some can modify the following code so that it be done in keypress event.
Thanks in advance!
If Asc(e.KeyChar) <> Keys.Back Then
If e.KeyChar.IsNumber(e.KeyChar) = False Then
If e.KeyChar <> "." Then
ze.Handled = True
Else
If txtAmountFigure.Text.IndexOf(".") <> -1 Then
e.Handled = True
End If
End If
End If
End If
"patang" <pa****@discussions.microsoft.com> schrieb:I have written the following code which allows user to enter only numbers
and
only one decimal point. This works fine.
However, I want that user shouldn''t enter a third number after decimal
point, for example user should not be allowed to enter 12557.234. User
should
only be allowed to enter two digits after decimal point.
I know I can validate it in the validate event, however, I was wondering
if
some can modify the following code so that it be done in keypress event.
I don''t think that this is a good idea. Validation should be made when the
control is validated (''Validating'' event), not when the user enters text.
This allows the user to do in-place editing of content pasted from the
clipboard, for example. Typically an error provider is set if the text
entered into the textbox is invalid.
--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>
Put this into the KeyPress event:
Dim KeyAscii As Integer
KeyAscii = Asc(e.KeyChar)
Select Case KeyAscii
Case 48 To 57, 8, 13
Case 45
If InStr(Me.Text, "-") <> 0 Then
KeyAscii = 0
End If
If Me.SelectionStart <> 0 Then
KeyAscii = 0
End If
Case 46
If InStr(Me.Text, ".") <> 0 Then
KeyAscii = 0
End If
Case Else
KeyAscii = 0
End Select
If KeyAscii = 0 Then
e.Handled = True
Else
e.Handled = False
End If
This accepts a decimal point, a minus sign too.
If Herfried wants to be pedantic about the above code then I will post the
code to stop all copy/paste/cut shorcut handlers too because Herfried once
before mentioned it. Although, he never produced a solution.
这篇关于仅文本框中的数字值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!