Public Sub Text1_KeyPRESS(Index As Integer, Keyascii As Integer)
Dim j As Integer
j = 0
Do
Text1(j).IMEMode = 3 '全角转换成半角,也就是小数点不能变成句号
If Keyascii < 45 Or Keyascii > 57 Or Keyascii = 47 Xor Keyascii = 8 Xor Keyascii = 13 Then  '限制输入没用字符
Keyascii = 0
End If
'禁止负号重复写If Text1(j).SelStart > 0 And Keyascii = 45 And Text1(j).SelLength < Len(Text1(j)) Then
Keyascii = 0
End If
If Left(Text1(j), 1) = "-" And Keyascii = 45 And Text1(j).SelLength < Len(Text1(j)) Then
Keyascii = 0
End If
If InStr(Text1(j), ".") > 0 And Keyascii = 46 And Text1(j).SelLength < Len(Text1(j)) Then Keyascii = 0 '禁止小数点重复写
If Keyascii = 13 And Index = j Then
If j = 3 Then
Text1(0).SetFocus
Exit Sub
End IfText1(j + 1).SetFocus
End If
j = j + 1
Loop Until j = Text1.Count
End Sub我写了以上代码,是想让文本框中只能输入数字,小数点每个文本框只能输入一次,负号每个文本框只能输入一次,且只能在第一个。现在的问题是所有文本框总共只能输入一个小数点一个负号。怎么解决呢?

解决方案 »

  1.   

    要简单点在文本框控件失去焦点时检查:
    private sub text1_lostfocus()
        if trim(text1.text)<>"" then
            if not isnumeric(text1.text) then
                msgbox "请输入数值型数据!",48,"提示"
                text1.text=""
                text1.setfocus
            end if
        end if
    end sub
      

  2.   


    dim Bak() as dobule  '在窗体 load 事件中,重新定义数组与 Text1 的控件数组总数相等Private Sub Text1_Change(Index As Integer) 
      if not isnumeric(Text1(index).text) then
         text1(index).text=bak(Index)
      else
         bak(Index)=text1(index)
      end if  
    End Sub
      

  3.   


    Option Explicit
    Dim Bak() As DoublePrivate Sub Form_Load()
      ReDim Bak(Text1.Count) As Double
    End SubPrivate Sub Text1_Change(Index As Integer)
        Dim Pos As Long
        Pos = Text1(Index).SelStart
        If Not IsNumeric(Text1(Index)) Then
            Text1(Index).Text = Bak(Index)
        Else
            Bak(Index) = Text1(Index).Text
        End If
         Text1(Index).SelStart = Pos
    End Sub
      

  4.   


    意外发现,在进一步做程序时,加了后面的代码,前段程序没做任何改动,居然顺利实现我上面提出的要求了。高兴是高兴,可为什么呢?
    Public Sub Text1_KeyPRESS(Index As Integer, Keyascii As Integer)
    Dim j As Integer
    j = 0
    Do
    Text1(j).IMEMode = 3 '全角转换成半角,也就是小数点不能变成句号
    If Keyascii < 45 Or Keyascii > 57 Or Keyascii = 47 Xor Keyascii = 8 Xor Keyascii = 13 Then  '限制输入没用字符
    Keyascii = 0
    End If
    '禁止负号重复写If Text1(j).SelStart > 0 And Keyascii = 45 And Text1(j).SelLength < Len(Text1(j)) Then
    Keyascii = 0
    End If
    If Left(Text1(j), 1) = "-" And Keyascii = 45 And Text1(j).SelLength < Len(Text1(j)) Then
    Keyascii = 0
    End If
    If InStr(Text1(j), ".") > 0 And Keyascii = 46 And Text1(j).SelLength < Len(Text1(j)) Then Keyascii = 0 '禁止小数点重复写
    If Keyascii = 13 And Index = j Then
    If j = 3 Then
    Text1(0).SetFocus
    Exit Sub
    End IfText1(j + 1).SetFocus
    End If
    j = j + 1
    Loop Until j = Text1.CountEnd Sub
    Private Sub Text1_gotfocus(Index As Integer)
    Dim j As Integer
    j = 0
    Do
    Text1(j).SelStart = 0
    Text1(j).SelLength = Len(Text1(j))
    j = j + 1
    Loop Until j = Text1.Count
    End Sub