#DesignerProperty: Key: Mask, DisplayName: Mask, FieldType: String, DefaultValue: None, List: None|Left|Right, Description: Where to start the mask.
#DesignerProperty: Key: MaskText, DisplayName: MaskText, FieldType: String, DefaultValue: XXX.XXX.XXX-XX, Description: Mask format use X or 0 to indicate the characters that will be replaced.
Sub Class_Globals
...
Private m_Mask As String = ""
Private m_MaskText As String = ""
End Sub
Private Sub IniProps(Props As Map)
...
m_Mask = Props.GetDefault("Mask","")
m_MaskText = Props.GetDefault("MaskText","")
End Sub
Private Sub TextChanged(Text As String)
If m_Mask<>"None" And (m_MaskText.Contains("X") Or m_MaskText.Contains("0")) Then
Dim textMasked As String = applyMask(Text, m_MaskText, m_Mask)
If Text <> textMasked Then
xtf_TextField.Text = textMasked
xtf_TextField.SelectionStart = xtf_TextField.Text.Length
End If
End If
If g_Counter.Visible Then
If Text.Length > g_Counter.CounterMax Then
If xtf_TextField.IsInitialized Then
Dim SelectionStart As Int = xtf_TextField.SelectionStart
xtf_TextField.Text = Text.SubString2(0,g_Counter.CounterMax)
xtf_TextField.SelectionStart = Min(SelectionStart,xtf_TextField.Text.Length)
End If
If xtf_TextFieldPassword.IsInitialized Then
Dim SelectionStart As Int = xtf_TextFieldPassword.SelectionStart
xtf_TextFieldPassword.Text = Text.SubString2(0,g_Counter.CounterMax)
xtf_TextFieldPassword.SelectionStart = Min(SelectionStart,xtf_TextFieldPassword.Text.Length)
End If
If xtf_Multiline.IsInitialized Then
Dim SelectionStart As Int = xtf_Multiline.SelectionStart
xtf_Multiline.Text = Text.SubString2(0,g_Counter.CounterMax)
xtf_Multiline.SelectionStart = Min(SelectionStart,xtf_Multiline.Text.Length)
End If
Text = Text.SubString2(0,g_Counter.CounterMax)
End If
End If
If Text.Length = 0 Then
xlbl_ClearButton.Visible = False
xlbl_RevealButton.Visible = False
xlbl_Hint.Visible = g_Hint.Visible
Else
xlbl_ClearButton.Visible = m_ShowClearButton
xlbl_RevealButton.Visible = m_PasswordField And m_ShowRevealButton
xlbl_Hint.Visible = False
End If
xlbl_Counter.Text = Text.Length & "/" & g_Counter.CounterMax
TextChanged_Event(Text)
End Sub
Private Sub lettersAndNumbers(Text As String) As String
Dim enable As String = "abcdefghijklmnopqrstuvwxyzçáãàâäéèêëíìîïóòôõöúùûüçABCDEFGHIJKLMNOPQRSTUVWXYZÁÃÀÂÄÉÈÊËÍÌÎÏÓÒÔÕÖÚÙÛÜÜÇ0123456789"
Dim textClear As StringBuilder
textClear.Initialize
For i = 0 To Text.Length-1
Dim c As String = Text.CharAt(i)
If enable.Contains(c) Then
textClear.Append(c)
End If
Next
Return textClear.ToString
End Sub
Private Sub applyMask(Text As String, Mask As String, Start As String) As String
Dim textClear As String = lettersAndNumbers(Text)
Dim textMasked As StringBuilder
textMasked.Initialize
Log(textClear)
Try
'check how many zeros are in the mask -> verifica quantos zeros tem na mascara
Dim zeros As Int = 0
For i = 0 To (Mask.Length-1)
If Mask.CharAt(i)="0" Then
zeros = zeros + 1
End If
Next
If zeros > 0 Then
If IsNumber(textClear) Then
textClear = textClear.As(Long)
End If
End If
'if there are more zeros in the mask than characters in the text, add zeros to the text, until you get the number of zeros in the mask -> caso tem mais zeros na mascara do que caracteres no texto, adiciona zeros ao texto, até dar a quantidade de zeros da mascara
If zeros > textClear.Length Then
textClear = "000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000" & textClear
textClear = textClear.SubString(textClear.Length-zeros)
End If
If Start="Left" Then
For i = 1 To Mask.Length
Dim c As Char = Mask.CharAt(i-1)
If (c="X" And textClear.Length>0) Or (c="0" And textClear.Length>0) Then
Dim t As Char = textClear.CharAt(0)'get the last character -> pega o ultimo caracter
textClear = textClear.SubString(1)'remove the last character -> remove o ultimo caracter
textMasked.Append(t)
Else
If c="X" Or textClear.Length=0 Then
Exit
End If
textMasked.Append(c)
End If
Next
Else If Start="Right" Then
For i = Mask.Length To 1 Step -1
Dim c As Char = Mask.CharAt(i-1)
If (c="X" And textClear.Length>0) Or (c="0" And textClear.Length>0) Then
Dim t As Char = textClear.CharAt(textClear.Length-1)'get the last character -> pega o ultimo caracter
textClear = textClear.SubString2(0,textClear.Length-1)'remove the last character -> remove o ultimo caracter
textMasked.Insert(0, t)
Else
If c="X" Or textClear.Length=0 Then
Exit
End If
textMasked.Insert(0, c)
End If
Next
End If
Catch
Log(LastException)
End Try
Log(textMasked.ToString)
Return textMasked.ToString
End Sub