This sample shows fields for a phone number, a ZIP+4 code, an amount with the popup calculator, a percentage, a quantity, an IP address, a hexadecimal color, a license key, a PIN, a city with autocomplete and a file name with an ellipsis button, with options that change the culture, dark mode, high contrast, the input mode, the entry selection, the prompt character, the underlines and the colors for invalid contents.
The source code is located at C:\Program Files (x86)\Softelvdm\SftMask NET 3.0\Samples\VB\DemoSample\Form1.vb.
Imports System.Globalization
Imports Softelvdm.SftMaskNET
Imports Softelvdm.SftMaskNET.Controls
Public Class Form1
Private fields() As SftMask
Public Sub New()
InitializeComponent()
fields = New SftMask() {SftMaskPhone, SftMaskZip, SftMaskAmount, SftMaskPercent, SftMaskQuantity, SftMaskIP,
SftMaskColor, SftMaskKey, SftMaskPin, SftMaskCity, SftMaskFile}
SftMaskPhone.Text = "4079045200"
SftMaskZip.Text = "328011234"
SftMaskIP.Text = "192168001001"
' Numbers are set as values, so they do not depend on the culture's separators.
SftMaskAmount.Value = 1234.5D
SftMaskPercent.Value = 25
SftMaskQuantity.Value = 10
SftMaskColor.Value = &H2E8B57
' The city field suggests earlier entries, starting with a few cities.
SftMaskCity.AutoComplete.Seed(New String() {"Orlando", "Ocala", "Ocoee", "Oviedo", "Orange City", "Miami", "Tampa", "Tallahassee"})
For Each field As SftMask In fields
AddHandler field.Enter, AddressOf Field_Enter
AddHandler field.TextChanged, AddressOf Field_TextChanged
Next
For Each name As String In New String() {"en-US", "de-DE", "fr-FR", "hi-IN", "ja-JP"}
ComboCulture.Items.Add(CultureInfo.GetCultureInfo(name))
Next
ComboCulture.SelectedItem = CultureInfo.GetCultureInfo("en-US")
FillCombo(ComboDarkMode, GetType(DarkModeEnum), SftMaskPhone.DarkMode)
FillCombo(ComboHighContrast, GetType(HighContrastModeEnum), SftMaskPhone.HighContrastMode)
FillCombo(ComboInputMode, GetType(InputModeEnum), SftMaskPhone.InputMode)
FillCombo(ComboEntrySelect, GetType(EntrySelectEnum), SftMaskPhone.EntrySelect)
ComboPrompt.Items.AddRange(New Object() {"(space)", "_", "#", "*"})
ComboPrompt.SelectedIndex = 1
CheckUnderline.Checked = SftMaskPhone.PromptUnderline
ShowColor()
End Sub
' On the .NET Framework, a ComboBox in a container keeps its old font when the window moves to a monitor with
' another scale. Setting another font and then the inherited one again makes it use the new one.
Private Sub Form1_DpiChanged(ByVal sender As Object, ByVal e As DpiChangedEventArgs) Handles Me.DpiChanged
BeginInvoke(New MethodInvoker(Sub()
For Each c As Control In GroupOptions.Controls
If Not TypeOf c Is ComboBox Then Continue For
Using other As New Font(Font.FontFamily, Font.Size + 1, Font.Style, Font.Unit)
c.Font = other
c.Font = Nothing
End Using
Next
End Sub))
End Sub
Private Sub FillCombo(ByVal combo As ComboBox, ByVal enumType As Type, ByVal current As Object)
For Each value As Object In [Enum].GetValues(enumType)
combo.Items.Add(value)
Next
combo.SelectedItem = current
End Sub
Private Sub Field_Enter(ByVal sender As Object, ByVal e As EventArgs)
ShowValues(CType(sender, SftMask))
End Sub
Private Sub Field_TextChanged(ByVal sender As Object, ByVal e As EventArgs)
If sender Is ActiveControl Then
ShowValues(CType(sender, SftMask))
End If
End Sub
' The text of the field with the input focus, in two forms, and its value.
Private Sub ShowValues(ByVal field As SftMask)
If field.UseSystemPasswordChar Then
LabelValues.Text = "Text: (not shown)"
Return
End If
Dim value As String = "(none)"
If field.Value.HasValue Then
value = field.Value.Value.ToString(field.Culture)
End If
LabelValues.Text = "Text: """ + field.Text + """ TextNoLiterals: """ + field.TextNoLiterals + """ Value: " + value
End Sub
Private Sub SftMaskColor_ValueChanged(ByVal sender As Object, ByVal e As EventArgs) Handles SftMaskColor.ValueChanged
ShowColor()
End Sub
' The hexadecimal field holds an RGB color.
Private Sub ShowColor()
Dim rgb As Decimal? = SftMaskColor.Value
If rgb.HasValue Then
PanelColor.BackColor = Color.FromArgb(255, Color.FromArgb(CInt(rgb.Value)))
Else
PanelColor.BackColor = SystemColors.Control
End If
End Sub
' The ellipsis button of the file field chooses a file.
Private Sub SftMaskFile_EllipseClick(ByVal sender As Object, ByVal e As EventArgs) Handles SftMaskFile.EllipseClick
Using dlg As New OpenFileDialog()
dlg.CheckFileExists = False
dlg.FileName = SftMaskFile.Text
If dlg.ShowDialog(Me) = DialogResult.OK Then
SftMaskFile.Text = dlg.FileName
End If
End Using
End Sub
Private Sub ComboCulture_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles ComboCulture.SelectedIndexChanged
For Each field As SftMask In fields
field.Culture = CType(ComboCulture.SelectedItem, CultureInfo)
Next
End Sub
Private Sub ComboDarkMode_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles ComboDarkMode.SelectedIndexChanged
For Each field As SftMask In fields
field.DarkMode = CType(ComboDarkMode.SelectedItem, DarkModeEnum)
Next
End Sub
Private Sub ComboHighContrast_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles ComboHighContrast.SelectedIndexChanged
For Each field As SftMask In fields
field.HighContrastMode = CType(ComboHighContrast.SelectedItem, HighContrastModeEnum)
Next
End Sub
Private Sub ComboInputMode_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles ComboInputMode.SelectedIndexChanged
For Each field As SftMask In fields
field.InputMode = CType(ComboInputMode.SelectedItem, InputModeEnum)
Next
End Sub
Private Sub ComboEntrySelect_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles ComboEntrySelect.SelectedIndexChanged
For Each field As SftMask In fields
field.EntrySelect = CType(ComboEntrySelect.SelectedItem, EntrySelectEnum)
Next
End Sub
Private Sub ComboPrompt_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles ComboPrompt.SelectedIndexChanged
Dim prompt As Char = " "c
If ComboPrompt.SelectedIndex > 0 Then
prompt = CStr(ComboPrompt.SelectedItem)(0)
End If
For Each field As SftMask In fields
field.PromptChar = prompt
Next
End Sub
Private Sub CheckPromptNoFocus_CheckedChanged(ByVal sender As Object, ByVal e As EventArgs) Handles CheckPromptNoFocus.CheckedChanged
For Each field As SftMask In fields
field.ShowPromptNoFocus = CheckPromptNoFocus.Checked
Next
End Sub
Private Sub CheckUnderline_CheckedChanged(ByVal sender As Object, ByVal e As EventArgs) Handles CheckUnderline.CheckedChanged
For Each field As SftMask In fields
field.PromptUnderline = CheckUnderline.Checked
Next
End Sub
' Fields whose contents are incomplete or out of range are shown in other colors.
Private Sub CheckInvalid_CheckedChanged(ByVal sender As Object, ByVal e As EventArgs) Handles CheckInvalid.CheckedChanged
For Each field As SftMask In fields
field.InvalidBackColor = If(CheckInvalid.Checked, Color.MistyRose, Color.Empty)
field.InvalidForeColor = If(CheckInvalid.Checked, Color.DarkRed, Color.Empty)
Next
End Sub
End Class