仅允许文本框中的数值

时间:2010-12-22 04:14:32

标签: vb6 textbox numbers

我想创建一个只接受数值的TextBox控件。

如何在VB6中执行此操作?

12 个答案:

答案 0 :(得分:8)

右键单击控制框>组件>控制 - > Microsoft Masked Edit Control 6.0。
或者使用普通文本框:

Private Sub Text1_Validate(Cancel As Boolean)
 Cancel = Not IsNumeric(Text1.Text)

End Sub

答案 1 :(得分:6)

在文本框文本Change事件中,检查输入的值是否为数字。如果它不是数字,则再次设置旧值。

Dim textval As String
Dim numval As String

Private Sub TextBox1_Change()
  textval = TextBox1.Text
  If IsNumeric(textval) Then
    numval = textval
  Else
    TextBox1.Text = CStr(numval)
  End If
End Sub

答案 2 :(得分:3)

我让API为我做。我将此函数添加到.bas模块并调用它以用于我需要设置为仅数字的任何编辑控件。

Option Explicit

Private Const ES_NUMBER = &H2000&
Private Const GWL_STYLE = (-16)
Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long


'set an editbox to numeric only - return the previous
'style on success or zero on error
Public Function ForceNumeric(ByVal EditControlhWnd As Long) As Long
    Dim lngCurStyle As Long
    Dim lngReturn As Long

    lngCurStyle = GetWindowLong(EditControlhWnd, GWL_STYLE)
    If lngCurStyle <> 0 Then
        lngReturn = SetWindowLong(EditControlhWnd, GWL_STYLE, lngCurStyle Or ES_NUMBER)
    End If

    ForceNumeric = lngReturn

End Function

要使用它,请使用TextBox的句柄调用该函数。

Private Sub Form_Load()
    Dim lngResult As Long

    lngResult = ForceNumeric(Text1.hwnd)

End Sub

答案 3 :(得分:1)

检查出来:

http://www.vbforums.com/showthread.php?t=350067

您需要检查每个按键,或者最后可以进行一次验证。

答案 4 :(得分:1)

我在项目中使用了以下代码:

Private Sub txtReceiptID_KeyPress(KeyAscii As Integer)
Dim Keychar As String
If KeyAscii > 31 Then
    Keychar = Chr(KeyAscii)
    If Not IsNumeric(Keychar) Then
        KeyAscii = 0
    End If
End If

结束子

答案 5 :(得分:0)

我通常使用这段代码:

Private Sub text1_KeyPress(KeyAscii As Integer)
    If Not IsNumeric(text1.Text & Chr(KeyAscii)) And Not KeyAscii = 8 Then KeyAscii = 0
End Sub

答案 6 :(得分:0)

以下内容可用于整数:

Private Sub text1_KeyPress(KeyAscii As Integer)
    If Not IsNumeric(text1.Text & Chr(KeyAscii)) And Not KeyAscii = 8 Then    KeyAscii = 0
    if (KeyAscii>=43) and (KeyAscii<=46) Then KeyAscii = 0 
    'it ignores '-', '+', '.' and ','
End Sub

答案 7 :(得分:0)

我通常使用此代码:

Private Sub text1_KeyPress(KeyAscii As Integer)
    If Not IsNumeric(Chr(KeyAscii)) And Not KeyAscii = 8 Then
        KeyAscii = 0
    End If
End Sub

希望这有帮助。

答案 8 :(得分:0)

试试这段代码:

Private Sub Text1_Change()
    textval = Text1.Text
    If IsNumeric(textval) Then
        numval = textval
    Else
        Text1.Text = CStr(numval)
    End If
End Sub

答案 9 :(得分:0)


只需选择控件和keyPress方法,IDE就会为您创建下一个方法。然后在方法中添加下一个代码

Private Sub txtControl_KeyPress(KeyAscii As Integer)
   KeyAscii = RealKeyascii(txtControl, KeyAscii, 256 ^ 8)
End Sub

答案 10 :(得分:-1)

If Len(Text2.Text) > 0 Then
    If IsNumeric(Text2.Text) = False Then
        Text2.SetFocus
        CreateObject("WScript.Shell").SendKeys "{BACKSPACE}"
    End If
End If

答案 11 :(得分:-1)

请尝试此代码:禁用字母(A-Z,a-z)并接受后缀,点,逗号和数字(0-9)=)

私有子Text1_KeyPress(KeyAscii作为整数)
如果不是(KeyAscii> = vbKeyA和KeyAscii <= vbKeyZ)否(KeyAscii> = 97和KeyAscii <= 122)然后
其他
KeyAscii = 0
如果结束
结束Sub