我自己定义了一个NumericTextBox的ActiveX控件(输入double类型数字)
Private Sub txtValue_KeyPress(KeyAscii As Integer)
Static bDecimalPointUsedUp As Boolean
Static bMinusSignAlready As Boolean
Select Case KeyAscii
Case vbKeyBack, vbKeyRight, vbKeyLeft
'什么都不做
Case Asc("0") To Asc("9")
'什么都不做
Case Asc(".")
If bDecimalPointUsedUp Then
KeyAscii = 0
RaiseEvent BadKey
Else
bDecimalPointUsedUp = True
End If
Case Else
KeyAscii = 0
RaiseEvent BadKey
End Select
End Sub
上面这段代码是确保用户只输入一次"."可是如果我删除小数点后,就不能再输入小数点了
因为bDecimalPointUsedUp = True应该在什么时候bDecimalPointUsedUp = False呢??
Private Sub txtValue_KeyPress(KeyAscii As Integer)
Static bDecimalPointUsedUp As Boolean
Static bMinusSignAlready As Boolean
Select Case KeyAscii
Case vbKeyBack, vbKeyRight, vbKeyLeft
'什么都不做
Case Asc("0") To Asc("9")
'什么都不做
Case Asc(".")
If bDecimalPointUsedUp Then
KeyAscii = 0
RaiseEvent BadKey
Else
bDecimalPointUsedUp = True
End If
Case Else
KeyAscii = 0
RaiseEvent BadKey
End Select
End Sub
上面这段代码是确保用户只输入一次"."可是如果我删除小数点后,就不能再输入小数点了
因为bDecimalPointUsedUp = True应该在什么时候bDecimalPointUsedUp = False呢??
Private Sub txtValue_KeyDown(KeyCode As Integer, Shift As Integer)
if keycode=46 then
'进一步判断是否有“.”被删除
……
end if
End Sub
If instr(1,txtValue.Text,".")>0 Then '存在小数点
Else
End If
Case Asc(".")
If instr(txtValue,".")<>0 Then
keyascii=0
RaiseEvent BadKey
End If