VB中的Text控件怎么才能让用户只能输入数字而不能输入其他的呢?
3个回答
展开全部
Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Private Const GWL_STYLE = (-16)
Private Const ES_NUMBER As Long = &H2000&
Private Sub Form_Load()
Dim style As Long
Text1.Text = ""
style = GetWindowLong(Text1.hwnd, GWL_STYLE)
SetWindowLong Text1.hwnd, GWL_STYLE, style Or ES_NUMBER
End Sub
用API好处是,输入的不是数字时,会自动提示。
Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Private Const GWL_STYLE = (-16)
Private Const ES_NUMBER As Long = &H2000&
Private Sub Form_Load()
Dim style As Long
Text1.Text = ""
style = GetWindowLong(Text1.hwnd, GWL_STYLE)
SetWindowLong Text1.hwnd, GWL_STYLE, style Or ES_NUMBER
End Sub
用API好处是,输入的不是数字时,会自动提示。
展开全部
Private Sub Text1_Change()
If IsNumeric(Text1.Text) = False Then
MsgBox "文本框输入了非法字符!", 16, "错误!"
Text1.Text = ""
Text1.SetFocus
End If
End Sub
If IsNumeric(Text1.Text) = False Then
MsgBox "文本框输入了非法字符!", 16, "错误!"
Text1.Text = ""
Text1.SetFocus
End If
End Sub
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
请问使用的是VB6吗?参考以下代码:
Sub Text1_KeyPress(KeyAscii As Integer)
If KeyAscii < 48 Or KeyAscii > 57 Then
KeyAscii = 0
End If
End Sub
数字正数
Sub Text1_KeyPress(KeyAscii As Integer)
If KeyAscii < 48 Or KeyAscii > 57 Then
KeyAscii = 0
End If
End Sub
数字正数
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询