KeyPress事件 控制输入的是整数还是小数
代码
''' <summary>
''' 限制输入字符串
''' Add by pwm 2010.07.18
''' </summary>
''' <param name="txtb">TextBox控件</param>
''' <param name="e">KeyPree事件</param>
''' <param name="flag">falg为True表示只能输入整数,为False表示小数或者整数</param>
''' <remarks></remarks>
Public Shared Sub LimitIntOrDbl(ByVal txtb As TextBox, ByVal e As KeyPressEventArgs, ByVal flag As Boolean)
'允许输入的字符
Dim AstrictChar As String
If flag Then
AstrictChar = "0123456789"
Else
AstrictChar = "0123456789."
End If
'BackSpace」「Delete」后退键正常删除操作
If CType(AscW(e.KeyChar), Keys) = Keys.Back Or CType(AscW(e.KeyChar), Keys) = Keys.Delete Then
Return
End If
'「Ctrl+C」(3)「Ctrl+X」(24)特殊组合键正常
'「Ctrl+Z」(26) 撤消组合键正常
If AscW(e.KeyChar) = 3 Or AscW(e.KeyChar) = 24 Or AscW(e.KeyChar) = 26 Then
Return
End If
'允许输入的字符外
If AstrictChar.IndexOf(e.KeyChar) = -1 Then
e.Handled = True
Return
End If
End Sub