'根据KeyAscii范围
Private Sub Text1_GotFocus()
SelTextBox Text1
End Sub
Private Sub Text1_KeyPress(KeyAscii As Integer)
If KeyAscii > 58 Or KeyAscii < 47 Then
MsgBox "Error", vbOKOnly, vbExclamation
Call Text1_GotFocus
End If
End Sub
Sub SelTextBox(tb As TextBox)
tb.SelStart = 0 ' Set selection start.
tb.SelLength = Len(tb.Text) ' Set selection length.
End Sub
'text框的 keypress事代码
'在有需要的地方这样调用 presskey 就可以了
Private Sub text1_KeyPress(KeyAscii As Integer)
PressKey KeyAscii
End Sub
'按键处理
'如果需要输入小数点正负号,请改变常量 allowkey
private Sub PressKey(ByRef keyVal As Integer)
const allowKey As String="0123456789"
If InStr(allowKey, Chr(keyVal)) = 0 And keyVal <> 8 Then keyVal = 0
End Sub