1

用户在txtAmountTextBox 中提供值,我稍后以这种方式处理:

c = txtAmount.Text 'GET AN ERROR HERE BECAUSE I NEED C TO BE AN  INT

我希望用户能够更正输入的值,所以我可以得到c一个整数:

If Not (IsNumeric(txtAmount.Text)) Then
  lblMsg.Caption = " Please enter you answer numericly "
  txtAmount.Text = ""
  txtAmount.SetFocus 'HERE IS WHAT I WANT TO FIX
End If
If (txtAmount.Text = "") Then
  lblMsg.Caption = " Please enter amount to convert"
  txtAmount.SetFocus
End If

在处理输入的值之前,我应该在哪里放置验证码以便调用它?

4

4 回答 4

1

下面是一个示例项目来说明我的意思:

'1 form with:
'  1 textbox: name=Text1
Option Explicit

Private Sub Text1_KeyPress(KeyAscii As Integer)
  KeyAscii = NrOnly(KeyAscii)
End Sub

Private Function NrOnly(intKey As Integer) As Integer
  Dim intReturn As Integer
  intReturn = intKey
  Select Case intKey
    Case vbKeyBack        'allow backspace
    Case vbKey0 To vbKey9 'allow numbers 0 to 9
    Case Else             'block all other input
      intReturn = 0
  End Select
  NrOnly = intReturn
End Function
于 2013-04-15T05:59:33.630 回答
1

您可以选择在键入时对其进行验证。为此,请使用如下更改事件:

Private Sub txtAmount_Change()

    On Error GoTo ErrManag

    If txtAmount.Text = "" Then Exit Sub
    If Not ((CInt(txtAmount.Text) >= 0) And (CInt(txtAmount.Text) <= 10)) Then MsgBox " Some max-min-error message"

ErrManag:

    If Err.Description <> "" Then
        MsgBox Err.Description 'This will prompt the error if a char is inserted
    End If

End Sub

在此示例中,用户将有机会更正它,但如果他没有,您必须稍后在单击“发送”按钮时重新检查它,就像 Hrqls 建议的那样。

于 2013-04-15T09:52:58.903 回答
0
Private Sub txtName_Validate(Cancel As Boolean)
   If txtName.Text = "" Then
      MsgBox "Please enter your name!", vbExclamation, ""
      Cancel = True
   End If
End Sub

当 Cancel 参数为True时,输入焦点回到txtName控件。

Private Sub txtPhone_Validate(Cancel As Boolean)
   If Len(txtPhone.Text) < 10 Then
      MsgBox "Enter the phone number in 10 digits!", vbExclamation, ""
      Cancel = True
   End If
End Sub

txtPhone在这种情况下,当 Cancel 参数设置为 时,输入焦点会移回控件True

于 2016-09-16T06:20:46.007 回答
0

您可以在文本框控件的验证事件中输入语句。在输入框中时,它会自动验证文本框中的数据,以便可以识别插入的数据

于 2018-12-06T09:08:42.510 回答