Option Explicit 'private variables Private WithEvents TxtBox As TextBoxPrivate blnKey As BooleanPrivate intSelStart As Integer'Public member Public IsDecimal As txtStylePublic Enum txtStyle Format_Zero = 0' only numbers and decimal format 100000 Format_two = 1 'format 100000.00 Format_Comma = 2' format 100,000.00 Fromat_Decimal = 3 'format can only be alpha End Enum
Private sub txtbox_change () 'function: Format Digital DIM BLN AS Booleandim i as integerim n () AS STRINGDIM M () AS STRINGDIM A AS INTEGER' Record Length DIM B AS INTEGER 'record length
SELECT CASDECIMAL CASE FORMAT_TWO if len (txtbox.text) = 4 dam = true i = txtbox.selstart 'Determine the input cursor position txtbox = format (txtbox.text, "###. 00") txtbox.selstart = i ix BLN = TRUE THEN TXTBOX.SELSTART = 1 case format_comma if len (txtbox.text) = 4 dam = true n = split (txtbox.text, ",") 'Determines the input cursor position a = len (txtbox.text)' Record length txtbox = format (txtbox.text, "##, ###. 00") b = len (txtbox.text) 'Record length m = split (txtbox.text, ",") IF (Ubound (M) > Ubound (n)) and ubound (m) <> 0 and a
Debug.Print "k"; k 'Debug.Print TxtBox.SelStart If bln = True Then TxtBox.SelStart = 1 End SelectEnd SubPrivate Sub TxtBox_GotFocus ()' function: select all text in the text box TxtBox.SelStart = 0TxtBox.SelLength = Len (txtbox.text) End Sub
Private sub txtbox_keydown (keycode as integer) function: Prevent two 00 DIM N AS INTEGER DIM M AS INTEGER DIM X () as string n = instr (1, txtbox.text, "when deleting a decimal point. ") 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 Ten 'decimal point INTSELSTART = INTSELSTART - 1 Else IntselStart = TXTBOX.SELSTART - 1 IF N = IntselStart - 1 THEN TXTBOX.SELSTART = IIF (intSelstart - 2 <0, 0, intSelstart - 2) end if Endiff 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 IntselStartend Sub
Private Sub TxtBox_keypress (KeyAscii As Integer) 'function: limiting the input Select Case IsDecimal Case Format_Comma, Format_two, Format_Zero' only numbers and decimal Dim intN As Integer intN = InStr Select Case KeyAscii (1, TxtBox.Text, ".") Case 8 '退 格 c Case ASC (".")' Decimal point if Intn <> 0 Then 'decimal point txtbox.selstart = INTN: keyascii = 0 elseif txtbox.selstart = 0 Then' in the first character 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 'key letters Case Else KeyAscii = 0 End Select End Select' If KeyAscii <> 8 And asc (".") <> 0 THEN INTSELSTART = TXTBOX.SELSTARTEND SUBPUBLIC PROPERTY LET SetTxt (Byval VNewValue As Variant) 'Features: Get Objects