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\CSharp\DemoSample\Form1.cs.
using System;
using System.Drawing;
using System.Globalization;
using System.Windows.Forms;
using Softelvdm.SftMaskNET;
using Softelvdm.SftMaskNET.Controls;
namespace DemoSample {
public partial class Form1 : Form {
private SftMask[] fields;
public Form1() {
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.5m;
sftMaskPercent.Value = 25;
sftMaskQuantity.Value = 10;
sftMaskColor.Value = 0x2E8B57;
// 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" });
foreach (SftMask field in fields) {
field.Enter += Field_Enter;
field.TextChanged += Field_TextChanged;
}
foreach (string name in new string[] { "en-US", "de-DE", "fr-FR", "hi-IN", "ja-JP" })
comboCulture.Items.Add(CultureInfo.GetCultureInfo(name));
comboCulture.SelectedItem = CultureInfo.GetCultureInfo("en-US");
FillCombo(comboDarkMode, typeof(DarkModeEnum), sftMaskPhone.DarkMode);
FillCombo(comboHighContrast, typeof(HighContrastModeEnum), sftMaskPhone.HighContrastMode);
FillCombo(comboInputMode, typeof(InputModeEnum), sftMaskPhone.InputMode);
FillCombo(comboEntrySelect, typeof(EntrySelectEnum), sftMaskPhone.EntrySelect);
comboPrompt.Items.AddRange(new object[] { "(space)", "_", "#", "*" });
comboPrompt.SelectedIndex = 1;
checkUnderline.Checked = sftMaskPhone.PromptUnderline;
ShowColor();
DpiChanged += Form1_DpiChanged;
}
// 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 void Form1_DpiChanged(object sender, DpiChangedEventArgs e) {
BeginInvoke(new MethodInvoker(delegate {
foreach (Control c in groupOptions.Controls) {
if (!(c is ComboBox)) continue;
using (Font other = new Font(Font.FontFamily, Font.Size + 1, Font.Style, Font.Unit)) {
c.Font = other;
c.Font = null;
}
}
}));
}
private void FillCombo(ComboBox combo, Type enumType, object current) {
foreach (object value in Enum.GetValues(enumType))
combo.Items.Add(value);
combo.SelectedItem = current;
}
private void Field_Enter(object sender, EventArgs e) {
ShowValues((SftMask)sender);
}
private void Field_TextChanged(object sender, EventArgs e) {
if (sender == ActiveControl)
ShowValues((SftMask)sender);
}
// The text of the field with the input focus, in two forms, and its value.
private void ShowValues(SftMask field) {
if (field.UseSystemPasswordChar) {
labelValues.Text = "Text: (not shown)";
return;
}
labelValues.Text = "Text: \"" + field.Text + "\" TextNoLiterals: \"" + field.TextNoLiterals + "\" Value: " +
(field.Value.HasValue ? field.Value.Value.ToString(field.Culture) : "(none)");
}
private void sftMaskColor_ValueChanged(object sender, EventArgs e) {
ShowColor();
}
// The hexadecimal field holds an RGB color.
private void ShowColor() {
decimal? rgb = sftMaskColor.Value;
panelColor.BackColor = rgb.HasValue ? Color.FromArgb(255, Color.FromArgb((int)rgb.Value)) : SystemColors.Control;
}
// The ellipsis button of the file field chooses a file.
private void sftMaskFile_EllipseClick(object sender, EventArgs e) {
using (OpenFileDialog dlg = new OpenFileDialog()) {
dlg.CheckFileExists = false;
dlg.FileName = sftMaskFile.Text;
if (dlg.ShowDialog(this) == DialogResult.OK)
sftMaskFile.Text = dlg.FileName;
}
}
private void comboCulture_SelectedIndexChanged(object sender, EventArgs e) {
foreach (SftMask field in fields)
field.Culture = (CultureInfo)comboCulture.SelectedItem;
}
private void comboDarkMode_SelectedIndexChanged(object sender, EventArgs e) {
DarkModeEnum mode = (DarkModeEnum)comboDarkMode.SelectedItem;
foreach (SftMask field in fields)
field.DarkMode = mode;
}
private void comboHighContrast_SelectedIndexChanged(object sender, EventArgs e) {
foreach (SftMask field in fields)
field.HighContrastMode = (HighContrastModeEnum)comboHighContrast.SelectedItem;
}
private void comboInputMode_SelectedIndexChanged(object sender, EventArgs e) {
foreach (SftMask field in fields)
field.InputMode = (InputModeEnum)comboInputMode.SelectedItem;
}
private void comboEntrySelect_SelectedIndexChanged(object sender, EventArgs e) {
foreach (SftMask field in fields)
field.EntrySelect = (EntrySelectEnum)comboEntrySelect.SelectedItem;
}
private void comboPrompt_SelectedIndexChanged(object sender, EventArgs e) {
char prompt = comboPrompt.SelectedIndex == 0 ? ' ' : ((string)comboPrompt.SelectedItem)[0];
foreach (SftMask field in fields)
field.PromptChar = prompt;
}
private void checkPromptNoFocus_CheckedChanged(object sender, EventArgs e) {
foreach (SftMask field in fields)
field.ShowPromptNoFocus = checkPromptNoFocus.Checked;
}
private void checkUnderline_CheckedChanged(object sender, EventArgs e) {
foreach (SftMask field in fields)
field.PromptUnderline = checkUnderline.Checked;
}
// Fields whose contents are incomplete or out of range are shown in other colors.
private void checkInvalid_CheckedChanged(object sender, EventArgs e) {
foreach (SftMask field in fields) {
field.InvalidBackColor = checkInvalid.Checked ? Color.MistyRose : Color.Empty;
field.InvalidForeColor = checkInvalid.Checked ? Color.DarkRed : Color.Empty;
}
}
}
}