HeaderPane
Main

Compare

A Windows Forms MaskedTextBox for numbers

The Windows Forms MaskedTextBox fills a fixed pattern of positions from left to right, which suits phone numbers and ZIP codes but not amounts, and NumericUpDown holds a single number with nothing around it. Here is how the two compare with SftMask/NET 3.0, a masked edit control with numeric fields.

The problem

A MaskedTextBox mask is a row of positions, each taking a digit, a letter or any character, with literals in between. That works for input of a fixed shape. An amount is not: with a mask such as 999,999.00 the user types into positions from the left, short numbers leave gaps where the optional digits are, the group separators sit where the mask puts them whatever the number's length, and nothing limits the value to a range. The usual workaround is a plain TextBox with parsing and validation code, or a NumericUpDown.

NumericUpDown handles one number well, with a range, decimal places and spin buttons, but that is all it holds. It has no mask, no literals, no place for a currency symbol or a unit, no value for "nothing entered yet", and no way to put several numbers in one field, such as the four parts of an IP address. Neither control suggests earlier entries while the user types.

SftMask/NET 3.0 is a masked edit control for Windows Forms, written entirely in C#, whose masks hold numeric fields along with the usual positions and literals.

Feature comparison

FeatureMaskedTextBoxNumericUpDownSftMask/NET 3.0
Edit masksFixed positions for digits, letters and any characters, with literalsNoThe same, plus lists and ranges of allowed characters and uppercase, lowercase and password blocks
Numbers of any lengthPositions filled from the leftOne numberDecimal, integer and hexadecimal fields, typed as on a calculator, with digit grouping, negative numbers and leading zeros
RangesNo, checked by your codeMinimum and MaximumA range for each numeric field
Several numbers in one fieldAs positions, without rangesNoSeveral numeric fields in one mask, such as the four parts of an IP address
Up and down buttonsNoYes, with accelerationYes, with acceleration, for each numeric field
CalculatorNoNoA popup calculator opens when an operator is typed into an amount
Currency symbol or unitA literal in the maskNoA label next to the input, including the culture's currency symbol
Nothing entered yetPrompt charactersNo, there is always a valuePrompt characters and underlined positions, and a null value
AutocompleteNoNoEarlier entries, files and folders
ValidationValidatingType and TypeValidationCompletedMinimum and MaximumRequired positions and ranges, your own checks, conversion to a type, and colors for invalid contents

What the code looks like

If you know the MaskedTextBox, much of SftMask/NET will look familiar: Mask, PromptChar, ValidatingType, TypeValidationCompleted and a Culture for the separators. It is not a drop-in replacement, though: the mask syntax is its own, with tokens for numeric fields, and a field's number is read and set as a nullable decimal. This sets up an amount with nine digits before the decimal separator and two after it, digit grouping, negative numbers, spin buttons, the popup calculator and the culture's currency symbol as its label, an IP address as four numeric fields from 0 to 255 that moves on to the next part by itself, and a city field that suggests earlier entries:

sftMaskAmount.Mask = "$^C-,9.2";
sftMaskAmount.Label = "|";
sftMaskAmount.ButtonStyle = ButtonStyleEnum.UpDown;
sftMaskAmount.Value = 1234.5m;

sftMaskIP.Mask = @"$I^03(0,255)\.$I^03(0,255)\.$I^03(0,255)\.$I^03(0,255)";
sftMaskIP.ButtonStyle = ButtonStyleEnum.UpDown;
sftMaskIP.AutoAdvance = true;

sftMaskCity.AutoComplete.File = @"-\Company\Product\City.autocomplete";
sftMaskCity.AutoComplete.Seed(new string[] { "Orlando", "Ocala", "Tampa", "Miami" });

You can also skip most of the code: the control is placed from the Visual Studio toolbox, and its property pages offer predefined masks and a field to try a mask at design time. The documentation lists where each MaskedTextBox and NumericUpDown function is found in SftMask/NET.

Modern Windows

SftMask/NET 3.0 runs in .NET Framework 4.7 (or later) and .NET 10 (or later) applications, with the same API and Visual Studio designer support in both. Set one property and the control, its calculator and its autocomplete list follow the Windows light/dark setting. The control scales with the DPI of the monitor showing it, honors the Windows contrast themes and has built-in UI Automation support for Narrator, NVDA, JAWS and other screen readers.

When the built-in controls are enough

If a form needs a phone number or a ZIP code, the MaskedTextBox is fine, and for a single quantity with spin buttons NumericUpDown is too, and both are free. SftMask/NET earns its place when users enter amounts, percentages or other numbers with a range and a symbol, when one field holds several numbers, when a field may stay empty, or when users should get their earlier entries back as they type.

Try it

The free trial includes the working control and the C# and VB.NET samples. Test it in your own forms before you buy. A license is royalty-free and includes one year of support and upgrades.

SftMask/NET 3.0 and free trial Documentation