有一个文本框txt1.text,它只能接受0--9的数字和小数点,其它的字符都不接受.请问代码要怎样写.在线等待.

解决方案 »

  1.   

    在change事件中用IsNumeric判断输入的是不是数字。
      

  2.   

    Private Sub Text1_KeyPress(KeyAscii As Integer)    If KeyAscii < 48 Or KeyAscii > 57 Then
            KeyAscii = 0
        End IfEnd Sub
      

  3.   

    Private Sub Text1_KeyPress(KeyAscii As Integer)
        Const xStr As String = "0123456789"
        KeyAscii = IIf(InStr(xStr, Chr(KeyAscii)), KeyAscii, 0)
    End Sub
      

  4.   

    大家都忽略一个问题:小数点,Enter键,删除键,Tab键都不能使用,请大家继续完成我的问题
      

  5.   

    補充:
    Private Sub Text1_KeyPress(KeyAscii As Integer)
        If KeyAscii < 48 Or KeyAscii > 57 Then
            If KeyAscii <> 46 Then
                KeyAscii = 0
            End If
         End If
    End Sub
    這樣不是OK了麼?
      

  6.   

    删除键和Del键还是不能用,请你再写一点好吗?
      

  7.   

    Public Function In_single(KeyAscii As Integer) As Boolean
    Dim ch_accept_single(20) As String
    ch_accept_single(0) = "0"
    ch_accept_single(1) = "1"
    ch_accept_single(2) = "2"
    ch_accept_single(3) = "3"
    ch_accept_single(4) = "4"
    ch_accept_single(5) = "5"
    ch_accept_single(6) = "6"
    ch_accept_single(7) = "7"
    ch_accept_single(8) = "8"
    ch_accept_single(9) = "9"
    ch_accept_single(10) = "."
    ch_accept_single(11) = "-"
    ch_accept_single(12) = Chr(8)
    In_single = False
    For i = 0 To 12
      If Chr(KeyAscii) = ch_accept_single(i) Then
        In_single = True
      End If
    Next
    End FunctionPrivate Sub Text1_KeyPress(KeyAscii As Integer)
        If In_single(KeyAscii) = False Then
          KeyAscii = 0
        End If
    End Sub
      

  8.   

    private sub text1_change()
       dim LastChar as string
       LastChar=right(text1.text,1)
       if isnumeric(LastChar)=false then
           Text1.text=left(text1.text,len(text1.text)-1)
       end if
    end sub
      

  9.   

    再次修改:    If (KeyAscii < 48 Or KeyAscii > 57) And KeyAscii <> 46 And KeyAscii <> 8 Then
            KeyAscii = 0
        End If呵呵,這樣沒問題了吧?
      

  10.   

    Private Sub Text1_KeyPress(KeyAscii As Integer)
        Dim xStr As String
        xStr = "0123456789." & Chr(8) & Chr(13)
        KeyAscii = IIf(InStr(xStr, Chr(KeyAscii)), KeyAscii, 0)
    End Sub
      

  11.   

    再加一句:
    If KeyAscii = 46 And InStr(Text1.Text, ".") > 0 Then
        KeyAscii = 0
    End If因為只能接受小數點一次。