各位高手好,我是初学者,有一小问题请教!
针对文本框内只能输入货币型数据,我写了以下代码,但小键盘却用不了,请帮忙修改!TKS!
Private Sub Text1_KeyUp(KeyCode As Integer, Shift As Integer)
Dim msg As String
If (KeyCode > 57 And KeyCode <> 190) Or (KeyCode > 33 And KeyCode < 48) Then
If Text1.Text <> "" Then Text1.Text = Left(Text1.Text, Len(Text1.Text) - 1)
msg = MsgBox("数据类型不对!", vbOKOnly, "错误!")
End If
End Sub

解决方案 »

  1.   

    需要允许96~105之间的KeyCode。
      

  2.   

    最好在validate 事件中处理Option Explicit
    Private Sub Text1_Validate(Cancel As Boolean)
      If Not IsNumeric(Text1) Then
        Text1 = 0
        Text1.SelLength = 1
        Cancel = True
      End If
    End Sub
      

  3.   

    子类化拦截复制粘贴用的是WM_COPY, WM_PASTE两个消息.2007年11月23日 星期五 08:38'子类化拦截复制粘贴用的是WM_COPY,   WM_PASTE两个消息.'窗体中代码    窗体中放个Text1
    Option ExplicitPrivate Sub Form_Load()
            PrevWndProc = SetWindowLong(Text1.Hwnd, GWL_WNDPROC, AddressOf SubWndProc)
    End SubPrivate Sub Form_Unload(Cancel As Integer)
            SetWindowLong Text1.Hwnd, GWL_WNDPROC, PrevWndProc
    End Sub'模块中代码
    Option ExplicitPublic Declare Function SetWindowLong Lib "user32 " Alias "SetWindowLongA " (ByVal Hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
    Public Declare Function CallWindowProc Lib "user32 " Alias "CallWindowProcA " (ByVal lpPrevWndFunc As Long, ByVal Hwnd As Long, ByVal MSG As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    Public Const GWL_WNDPROC = (-4)
    Public Const WM_GETTEXT = &HD
    Public Const WM_COPY       As Long = &H301
    Public Const WM_PASTE       As Long = &H302Public PrevWndProc     As LongPublic Function SubWndProc(ByVal Hwnd As Long, ByVal MSG As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
            Select Case MSG                       '在这里进行过滤.如果知道其他的消息,也可以在这里过滤.
                    Case WM_COPY, WM_PASTE
                            SubWndProc = 1
                            Exit Function
            End Select
            SubWndProc = CallWindowProc(PrevWndProc, Hwnd, MSG, wParam, lParam) '其它消息不管
    End Function
     
    'VB输入过滤 FFX.7799.CN
    Public Function CheckText(KeyIn As Integer, InValidateString As String, NY As Boolean, Editable As Boolean) As Integer
    '输入过滤
    'KeyIn              是KeyAscii值
    'InValidateString   字符列表
    'NY                 true :只能输入InValidateString中的字符
    '                   False:只能输入InValidateString中的没有的字符
    'Editable           只否可以使用编辑键
       On Error GoTo myerr
        Dim ValidateList As String
        Dim KeyOut As Integer
        If KeyIn < 0 Then
            CheckText = 0
            Beep
            Exit Function
        End If
        If Editable = True Then
             ValidateList = UCase(InValidateString) & Chr(8)
        Else
             ValidateList = UCase(InValidateString)
        End If
        If NY Then
          If InStr(1, ValidateList, UCase(Chr(KeyIn)), 1) > 0 Then
              KeyOut = KeyIn
          Else
              KeyOut = 0
              Beep
          End If     Else
          If InStr(1, ValidateList, UCase(Chr(KeyIn)), 1) = 0 Then
              KeyOut = KeyIn
          Else
              KeyOut = 0
              Beep
          End If
        End If
        CheckText = KeyOut
    Exit Function
    myerr:
    End Function
    Private Sub Text1_KeyPress(KeyAscii As Integer)
         KeyAscii = CheckText(KeyAscii, "1234567890", True, True)
    '或  KeyAscii = CheckText(KeyAscii, "1234567890",False , False)
    End Sub
     
      

  4.   


    Private   Sub   Text1_KeyPress(KeyAscii   As   Integer) 
              if keyascii <96 and keyascii > 105 then
                    if keyascii <> 8 then   KeyAscii   =  0
              endif
    End   Sub