The new class should check if the value is actually entered, and the class type should be set as the MaskedTextBox1 ValidatingType property, as shown below:
public class HexValidator
{
. . .
public static HexValidator Parse(string s)
{
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]);
}
}
maskedTextBox1.ValidatingType = typeof(HexValidator);
private void maskedTextBox1_TypeValidationCompleted(object sender, TypeValidationEventArgs e)
{
if (e.IsValidInput)
{
}
else
{
toolTip1.Show(e.Message, maskedTextBox1, maskedTextBox1.Location, 5000);
}
}
source
share