我自己定义了一个NumericTextBox的ActiveX控件(输入double类型数字)
Private Sub txtValue_KeyPress(KeyAscii As Integer)
    Static bDecimalPointUsedUp As Boolean
    Static bMinusSignAlready As Boolean
    Select Case KeyAscii
    Case vbKeyBack, vbKeyRight, vbKeyLeft
        '什么都不做
    Case Asc("0") To Asc("9")
        '什么都不做
    Case Asc(".")
        If bDecimalPointUsedUp Then
            KeyAscii = 0
            RaiseEvent BadKey
        Else
            bDecimalPointUsedUp = True
        End If
    Case Else
        KeyAscii = 0
        RaiseEvent BadKey
    End Select
End Sub
上面这段代码是确保用户只输入一次"."可是如果我删除小数点后,就不能再输入小数点了
因为bDecimalPointUsedUp = True应该在什么时候bDecimalPointUsedUp = False呢??

解决方案 »

  1.   

    删除时bDecimalPointUsedUp = False,或者碰到小数点时查找字符串中有没有".",有的话就不让输入,没有的话输入一个"."。
      

  2.   

    判断输入的“del”键的话
    Private Sub txtValue_KeyDown(KeyCode As Integer, Shift As Integer)
    if keycode=46 then
        '进一步判断是否有“.”被删除
       ……
    end if
    End Sub
      

  3.   

    Case Asc(".")
            If instr(1,txtValue.Text,".")>0 Then   '存在小数点
            Else            
            End If
      

  4.   

    或者修改……
    Case Asc(".")
            If instr(txtValue,".")<>0  Then 
                keyascii=0
                RaiseEvent BadKey
            End If