CSDN博客

img lang_csdn

让文本框只能输入指定格式的类

发表于2004/10/17 21:18:00  757人阅读

分类: VB6.0

Option Explicit
'私有变量
Private WithEvents TxtBox As TextBox
Private blnKey As Boolean
Private intSelStart As Integer
'Public成员
Public IsDecimal As txtStyle
Public Enum txtStyle
    Format_Zero = 0     '格式 100000 只能数字和小数点
    Format_two = 1      '格式 100000.00
    Format_Comma = 2    '格式 100,000.00
    Fromat_Decimal = 3  '格式 只能是字母
End Enum

Private Sub TxtBox_Change()
'功     能  : 格式化数字
Dim bln As Boolean
Dim i As Integer
Dim n() As String
Dim m() As String
Dim a As Integer '记录长度
Dim b As Integer '记录长度

    Select Case IsDecimal
        Case Format_two
            If Len(TxtBox.Text) = 4 Then bln = True
            i = TxtBox.SelStart '确定输入光标位置
                TxtBox = Format(TxtBox.Text, "###.00")
            TxtBox.SelStart = i
            If bln = True Then TxtBox.SelStart = 1
        Case Format_Comma
            If Len(TxtBox.Text) = 4 Then bln = True
            n = Split(TxtBox.Text, ",") '确定输入光标位置
            a = Len(TxtBox.Text) '记录长度
           
            TxtBox = Format(TxtBox.Text, "##,###.00")
           
            b = Len(TxtBox.Text) '记录长度
            m = Split(TxtBox.Text, ",")
            If (UBound(m) > UBound(n)) And UBound(m) <> 0 And a < b Then
                intSelStart = intSelStart + 1
            ElseIf (UBound(m) = UBound(n)) And UBound(m) <> 0 And a < b Then
                intSelStart = intSelStart
            End If
            If a > b Then '删除操作
                If (UBound(m) = UBound(n)) And UBound(m) <> 0 Then
                     intSelStart = intSelStart
                ElseIf (UBound(m) < UBound(n)) And UBound(m) <> 0 Then
                    intSelStart = intSelStart - 1
                ElseIf UBound(m) = 0 Then
                    intSelStart = intSelStart - 1
                End If
            End If
            TxtBox.SelStart = IIf(intSelStart < 0, 0, intSelStart)
            'Debug.Print "TxtBox.SelStart"; TxtBox.SelStart
            Dim k As Integer
            k = InStr(1, TxtBox.Text, ".")
            If TxtBox.SelStart = k Then TxtBox.SelStart = IIf(k - 1 < 0, 0, k - 1)
'            Debug.Print "k"; k
'            Debug.Print TxtBox.SelStart
            If bln = True Then TxtBox.SelStart = 1
       
    End Select
End Sub

Private Sub TxtBox_GotFocus()
'功 能  : 选择文本框中所有的文字
TxtBox.SelStart = 0
TxtBox.SelLength = Len(TxtBox.Text)
End Sub

Private Sub TxtBox_KeyDown(KeyCode As Integer, Shift As Integer)
'功 能  : 防止在删除小数点时出现两个00
 Dim n As Integer
 Dim m As Integer
 Dim x() As String
  n = InStr(1, TxtBox.Text, ".")
  Select Case IsDecimal
        Case Format_two
             If KeyCode = 46 And (TxtBox.SelStart + 1 = n) Then
            
                If TxtBox.SelStart - 1 >= 0 Then TxtBox.SelStart = TxtBox.SelStart - 1
             End If
           
             If KeyCode = 8 And (TxtBox.SelStart = n) Then
                If n - 1 >= 0 Then TxtBox.SelStart = n - 1
             End If
        Case Format_Comma
            x = Split(TxtBox.Text, ",")
            If KeyCode = 8 Then
                If intSelStart > n Then '小数点后
                    intSelStart = intSelStart - 1
                Else
                    intSelStart = TxtBox.SelStart - 1
                    If n = intSelStart - 1 Then
                        TxtBox.SelStart = IIf(intSelStart - 2 < 0, 0, intSelStart - 2)
                    End If
                End If
            ElseIf KeyCode = 46 Then
                If n - 1 = TxtBox.SelStart Then
                     TxtBox.SelStart = TxtBox.SelStart + 1
                     KeyCode = 0
                   
                Else
                    intSelStart = TxtBox.SelStart
                End If
            Else
                If UBound(x) = 0 And intSelStart = n Then
                    intSelStart = TxtBox.SelStart + 2
                Else
                    intSelStart = TxtBox.SelStart + 1
                End If
            End If
  End Select
'Debug.Print "SelStart"; TxtBox.SelStart & "  " & n
'Debug.Print intSelStart

End Sub

Private Sub TxtBox_keypress(KeyAscii As Integer)
'功 能  :  限制输入
  Select Case IsDecimal
    Case Format_Comma, Format_two, Format_Zero '只能是数字和小数点
        Dim intN As Integer
        intN = InStr(1, TxtBox.Text, ".")
        Select Case KeyAscii
            Case 8 '退格键
            Case Asc(".") '小数点
                If intN <> 0 Then    '小数点
                    TxtBox.SelStart = intN: KeyAscii = 0
                ElseIf TxtBox.SelStart = 0 Then '在第一字符
                    KeyAscii = 0
                End If
            Case Else
                If Not IsNumeric(Chr(KeyAscii)) Then KeyAscii = 0
        End Select
    Case Fromat_Decimal
        Select Case KeyAscii
            Case 8
            Case 65 To 90, 97 To 122 '按键为字母
            Case Else
                KeyAscii = 0
        End Select
  End Select
'  If KeyAscii <> 8 And Asc(".") <> 0 Then intSelStart = TxtBox.SelStart
End Sub
Public Property Let SetTxt(ByVal vNewValue As Variant)
'功 能  : 获取对象<Text1>
'参 数  : vNewValue 对象名
   Set TxtBox = vNewValue
End Property

阅读全文
0 0

相关文章推荐

img
取 消
img