Text1.MaxLength = 2Private Sub Text1_KeyPress(KeyAscii As Integer) If KeyAscii < Asc(" ") Then ' Is this Control char? Exit Sub ' Yes, let it pass End If If KeyAscii >= Asc("0") And KeyAscii <= Asc("9") Then ' keep digit ElseIf KeyAscii = Asc(".") Then ' keep . ElseIf KeyAscii = Asc("-") Then ' Keep - only if first char Else KeyAscii = 0 ' Discard all other chars End IfEnd Sub
我想是这样,不知可否:Private Sub Text1_KeyPress(KeyAscii As Integer) Dim potpos As Integer If KeyAscii = 13 Then '判断是否键入回车 If Text1.Text Like "*.##" Then Text1.Text = Text1.Text Else potpos = InStr(Text1.Text, ".") '定位“.”的位置 If potpos > 0 Then Text1.Text = Left(Text1.Text, potpos + 2) ElseIf potpos = 0 Then Text1.Text = Text1.Text & ".00"'如无小数,则补上两位小数 End If End If End If End Sub
Text1.MaxLength = 2Private Sub Text1_KeyPress(KeyAscii As Integer)
If KeyAscii < Asc(" ") Then ' Is this Control char?
Exit Sub ' Yes, let it pass
End If If KeyAscii >= Asc("0") And KeyAscii <= Asc("9") Then
' keep digit
ElseIf KeyAscii = Asc(".") Then
' keep .
ElseIf KeyAscii = Asc("-") Then
' Keep - only if first char
Else
KeyAscii = 0 ' Discard all other chars
End IfEnd Sub
使用like语句好像是 text1.text like "*.__"把,我忘记了
你的方法不行,用户要是输入37238-348.3434.234这种就无法判断。
我要的是用户只能输入*.__这样格式的值,如果即将输入的字符会破坏这种格式,那么就禁止。
shawls(小山)(无业游民)(VB版的众矢之的) :
谢谢,我忘了这个方法,试一试。
text1.text=format(text1.text,"##############0.00")
End sub
[email protected]
Dim potpos As Integer
If KeyAscii = 13 Then '判断是否键入回车
If Text1.Text Like "*.##" Then
Text1.Text = Text1.Text
Else
potpos = InStr(Text1.Text, ".") '定位“.”的位置
If potpos > 0 Then
Text1.Text = Left(Text1.Text, potpos + 2)
ElseIf potpos = 0 Then
Text1.Text = Text1.Text & ".00"'如无小数,则补上两位小数
End If
End If
End If
End Sub