这段代码使textbox1(2-8)只允许被输入数值,且数值不为负数,我想使text1(5)允许被输入负数0和正数(就是在原来的基础上增加了负数的输入),对其它textbox要求不变。
Private Sub Text1_KeyPress(Index As Integer, KeyAscii As Integer)
    If Index >= 2 And Index <= 8 Then
        '46 代表小数点 8 代表退格键  13 代表回车键
        If Not ((KeyAscii >= 48 And KeyAscii <= 57) Or KeyAscii = 46 Or KeyAscii = 8 Or KeyAscii = 13) Then
            KeyAscii = 0
        End If
    End If
End Sub

解决方案 »

  1.   

    '支持正数\负数\小数点的输入\负号只能出现在第一位\负号与小数点不能多次出现Private Sub Text1_KeyPress(KeyAscii As Integer)
        If KeyAscii <> 8 And KeyAscii <> 13 And (KeyAscii < 48 Or KeyAscii > 57) Then
            If KeyAscii = 46 Then
                If Text1.Text = "" Or InStr(1, Text1.Text, ".") <> 0 Then  '.号不能多次出现
                    KeyAscii = 0
                End If
            ElseIf KeyAscii = 45 Then
                If InStr(1, Text1.Text, "-") <> 0 Then     '-号不能多次出现
                    KeyAscii = 0
                End If
            Else
                KeyAscii = 0
            End If
        End If
    End SubPrivate Sub Text1_KeyUp(KeyCode As Integer, Shift As Integer)
       '防止负号在第1个字符以外的位置存在
       Dim i As Integer
       i = InStr(2, Text1.Text, "-")
       If i <> 0 Then
          Text1.Text = Mid(Text1.Text, 1, i - 1) + Mid(Text1.Text, i + 1, Len(Text1.Text))
          Text1.SelStart = i - 1
       End If
      
    End Sub