I need a control that only accept HEX value in the following format:
xxxx-xxxx and x is in 0-9, a-f, A-F
So I add a MaskedTextBox and try to edit its Mask property, but did not succeed.
I need a control that only accept HEX value in the following format:
xxxx-xxxx and x is in 0-9, a-f, A-F
So I add a MaskedTextBox and try to edit its Mask property, but did not succeed.
You can't do this out of the box with a MaskedTextBox
. If you take a look at the allowed masking elements detailed at the documentation for the Mask
property, you'll see that there is no way that you can enforce only hexadecimal input.
You can use a NumericUpDown
and set its Hexadecimal
property to true
, though.
A new class is need to verify if the value input is valid and the class type should be set as MaskedTextBox1's ValidatingType property, as following:
public class HexValidator
{
. . .
// the following function is needed to used to verify the input
public static HexValidator Parse(string s)
{
// remove any spaces
s = s.Replace(" ", "");
string[] strParts = s.Split('-');
int nValue;
foreach (string part in strParts)
{
bool result = int.TryParse(part, System.Globalization.NumberStyles.AllowHexSpecifier, null, out nValue);
if (false == result)
throw new ArgumentException(string.Format("The provided string {0} is not a valid subnet ID.", s));
}
return new SubnetID(strParts[0], strParts[1]);
}
}
/// set as following to associate them
maskedTextBox1.ValidatingType = typeof(HexValidator);
// in the validationcomplete event, you could judge if the input is valid
private void maskedTextBox1_TypeValidationCompleted(object sender, TypeValidationEventArgs e)
{
if (e.IsValidInput)
{
//AppendLog("Type validation succeeded. Message: " + e.Message);
}
else
{
toolTip1.Show(e.Message, maskedTextBox1, maskedTextBox1.Location, 5000);
}
}