Vb6 仅允许文本框中的数值

Vb6 仅允许文本框中的数值,vb6,textbox,numbers,Vb6,Textbox,Numbers,我想制作一个只接受数值的TextBox控件 如何在VB6中执行此操作?请查看: 您需要检查每个按键,或者可以在最后执行一次验证。在文本框textChange事件中,检查输入的值是否为数字。如果不是数字,则再次设置旧值 Dim textval As String Dim numval As String Private Sub TextBox1_Change() textval = TextBox1.Text If IsNumeric(textval) Then numval

我想制作一个只接受数值的TextBox控件

如何在VB6中执行此操作?

请查看:


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

在文本框text
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

右键单击控制框>组件>控制->Microsoft蒙版编辑控制6.0。
或使用普通文本框:

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

End Sub

我让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

我通常使用以下代码:

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

以下可用于整数:

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
Private Sub text1\u按键(keyscii为整数)
如果不是IsNumeric(text1.Text&Chr(keyscii))且不是keyscii=8,则keyscii=0

如果(keyscii>=43)和(keyscii我通常使用以下代码:

Private Sub text1_KeyPress(KeyAscii As Integer)
    If Not IsNumeric(text1.Text & Chr(KeyAscii)) And Not KeyAscii = 8 Then KeyAscii = 0
End Sub
Private Sub text1_KeyPress(KeyAscii As Integer)
    If Not IsNumeric(Chr(KeyAscii)) And Not KeyAscii = 8 Then
        KeyAscii = 0
    End If
End Sub
希望这有帮助。

尝试以下代码:

If Len(Text2.Text) > 0 Then
    If IsNumeric(Text2.Text) = False Then
        Text2.SetFocus
        CreateObject("WScript.Shell").SendKeys "{BACKSPACE}"
    End If
End If
Private Sub Text1_Change()
    textval = Text1.Text
    If IsNumeric(textval) Then
        numval = textval
    Else
        Text1.Text = CStr(numval)
    End If
End Sub

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

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

End Sub

尝试以下代码:禁用字母(A-Z,A-Z)并接受退格、点、逗号和数字(0-9)=

专用子文本1\u按键(KEYSCII为整数)
如果不是(keyscii>=vbKeyA和keyscii=97和keyscii
只需选择控件和按键方法,IDE为您创建下一个方法。然后在该方法中添加下一个代码

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

您宁愿使用Validate event(验证事件)。这要视情况而定。如果您希望在用户离开textbox控件并尝试将焦点设置为其他内容时触发仅输入数字的验证,则应使用
Validate
事件。如果您选择该路径,则只有到那时才会显示错误或通知。另一方面另一方面,如果您使用
Change
事件,用户将立即收到通知,他们键入的非数字值无效。但这也有其问题。您可以复制任何非数字文本,并通过(1)Ctrl-V、(2)Shift-Insert和(3)文本框的上下文菜单将其粘贴到控件中。仅(1)可以通过按键在VB6中寻址。@赫伯,你是对的,但防止将无效值粘贴到VB的文本框中的唯一方法是捕获系统消息。这是可能的,但在VB6中做的事情很难看。这只会阻止键入非数字值。如果我需要确保数据完整性,我会在使用它之前检查文本是否有效处理。同时,我让用户更难输入无效数据。在.Net中,我将此与监视WM_粘贴的消息流相结合(我认为)在允许之前,请检查要粘贴的信息。该问题是在8年前回答的,您的代码没有格式,因此难以阅读。请尝试回答新的未回答问题,而不是已接受答案的旧问题。是否要为您的coce only答案添加一些解释?这会有帮助认为StackOverflow是免费代码编写服务的误解。