Search Tools Links Login

Mask a Textbox


Visual Basic 6, or VB Classic

This function will turn any textbox into a MaskEdit box! Just call this function from the textbox Change event, or KeyUp events.
It may not seem like much, but it works, and I worked hard to figure this out just right. This is PURE VB code! Email me for a VB.NET version of this routine, although it is pretty easy to convert.
Please rate my submissions! I enjoy hearing feedback on my code. Thanks all.

Original Author: Chuck Potalivo

Inputs

A textbox object, and a mask string

Code

Private Function MaskText(txtTarget As VB.Textbox, strMask As String)
Static bolRunning   As Boolean

If bolRunning Then
Exit Function
End If

bolRunning = True
Dim strTarget_Text   As String
strTarget_Text = txtTarget.Text

Dim lngCursor_Pos   As Long
lngCursor_Pos = txtTarget.SelStart

If Len(strMask) > Len(strTarget_Text) Then
strTarget_Text = strTarget_Text & Space(Len(strMask) - Len(strTarget_Text))
ElseIf Len(strMask) < Len(strTarget_Text) Then
strTarget_Text = Left(strTarget_Text, Len(strMask))
ElseIf Len(strMask) = 0 Then
Exit Function
End If

Dim strTarget_Char   As String * 1
Dim strMask_Char   As String * 1

Dim strTemp     As String
Dim bolAlpha    As Boolean

Dim aryLiterals    As Variant
aryLiterals = Array("(", ")", "-", ".", ",", ":", ";", "/", "", " ")

Dim lngLiteral_Index  As Long
Dim bolLiteral    As Boolean

Dim lngChar_Index   As Long

For lngChar_Index = 1 To Len(strMask)
strTarget_Char = Mid(strTarget_Text, lngChar_Index, 1)
strMask_Char = Mid(strMask, lngChar_Index, 1)

For lngLiteral_Index = LBound(aryLiterals) To UBound(aryLiterals)
  bolLiteral = (strMask_Char = aryLiterals(lngLiteral_Index))
  
  If bolLiteral Then
  Exit For
  End If
Next lngLiteral_Index

Select Case strMask_Char
  Case "#":
  If (Not IsNumeric(strTarget_Char)) And (strTarget_Char <> " ") Then
   strTemp = Right(strTarget_Text, Len(strTarget_Text) - lngChar_Index)
  
   If lngChar_Index > 1 Then
   strTarget_Text = Left(strTarget_Text, lngChar_Index - 1)
   Else
   strTarget_Text = ""
   End If
  
   strTarget_Text = strTarget_Text & " " & strTemp
  End If
  Case "@":
  bolAlpha = ((Asc(strTarget_Char) >= 65) And (Asc(strTarget_Char) <= 90)) Or ((Asc(strTarget_Char) >= 97) And (Asc(strTarget_Char) <= 122))
  
  If (Not bolAlpha) And (strTarget_Char <> " ") Then
   strTemp = Right(strTarget_Text, Len(strTarget_Text) - lngChar_Index)
  
   If lngChar_Index > 1 Then
   strTarget_Text = Left(strTarget_Text, lngChar_Index - 1)
   Else
   strTarget_Text = ""
   End If
  
   strTarget_Text = strTarget_Text & " " & strTemp
  End If
  Case Else:
  If (strTarget_Char <> strMask_Char) And bolLiteral Then
   strTemp = Right(strTarget_Text, Len(strTarget_Text) - (lngChar_Index - 1))
  
   strTarget_Text = Left(strTarget_Text, lngChar_Index - 1)
   strTarget_Text = strTarget_Text & strMask_Char & strTemp
  
   If lngChar_Index = lngCursor_Pos Then
   lngCursor_Pos = lngCursor_Pos + 1
   End If
  End If
End Select
Next lngChar_Index

txtTarget.Text = Left(strTarget_Text, Len(strMask))
txtTarget.SelStart = lngCursor_Pos

bolRunning = False
End Function

About this post

Posted: 2002-06-01
By: ArchiveBot
Viewed: 130 times

Categories

Visual Basic 6

Attachments

No attachments for this post


Loading Comments ...

Comments

No comments have been added for this post.

You must be logged in to make a comment.