我想在TEXT.TEXT中只输入数子(包括小数点后两位含小数点)怎么写代码?

解决方案 »

  1.   

    2个方式:
    1. 找个别的控件。
    2. 自己在Change事件中写控制,每次改变的时候,如果不为数字就清除,即恢复修改前的原来的数字。
      

  2.   

    Private Sub Text1_KeyPress(KeyAscii As Integer)
    If KeyAscii <> 46 And Not (KeyAscii >= 48 And KeyAscii <= 57) Then
        KeyAscii = 0
    End If
    End Sub
      

  3.   

    我按照2楼的方式做发现有两个问题:
    1,小数点可以输入很多,我只想输入一个就可以了;
    2,我不能用去格(bk sp)键删除.
    怎么办?
      

  4.   

    private sub text1_lostfocus
        if trim(text1.text)<>"" then
            if not isnumeric(text1.text) then
                msgbox "请输入数值型数据!",48,"提示"
                text1.setfocus
            end if
        end if
    end sub
      

  5.   


    Private Sub Text1_Change()
            With Text1
                If InStr(1, StrReverse(.Text), ".") >= 4 Then
                    SendKeys "{BACKSPACE}"
                End If
            End With
    End SubPrivate Sub Text1_KeyPress(KeyAscii As Integer)
            With Text1
                If KeyAscii = 46 Or (KeyAscii >= 48 And KeyAscii <= 57) Then
                    If Len(Trim(.Text)) <> 0 Then
                        If InStr(1, StrReverse(.Text), ".") > 0 And KeyAscii = 46 Then
                            KeyAscii = 0
                        End If
                    End If
                End If
            End With
    End Sub
      

  6.   

    Private Sub Text1_KeyPress(KeyAscii As Integer)
        
        '// Check "."
        If (KeyAscii = 46) Then
            '// Ensure only one "."
            If (InStr(1, Text1.Text, Chr(KeyAscii)) > 0) Then
                KeyAscii = 0
            End If
            Exit Sub
        End If
        
        '// Check number "0-9"
        If (KeyAscii >= 48 And KeyAscii <= 57) Then
            Exit Sub
        End If
        
        '// Check Backspace key
        If (KeyAscii = 8) Then
            Exit Sub
        End If
        
        KeyAscii = 0
    End Sub
      

  7.   

    Private Sub text_KeyPress(KeyAscii As Integer)
        Call Number(KeyAscii)
    End SubPublic Sub Number(KeyAscii As Integer)
        Dim strValid As String
        strValid = "0123456789."
    End Sub我们的现在就是这么做的。
      

  8.   

    Private Sub Text1_KeyPress(KeyAscii As Integer)Select Case KeyAscii
        Case 8, 9 13, &H30 To &H 39
        Case Asc(".")
            If InStr(Text1, ".") Then KeyAscii = 0
        Case Else
            KeyAscii = 0
    End SelectEnd Sub
      

  9.   

    在Change事件中检测受到的当前字符是不是数字,不是就把它去掉。