我需要一个控制,只有在下列格式接受HEX值:如何使MaskedTextBox只接受HEX值?
XXXX-XXXX,x是在0-9,AF,AF
所以我添加了一个MaskedTextBox中并尝试编辑其面膜属性,但没有成功。
我需要一个控制,只有在下列格式接受HEX值:如何使MaskedTextBox只接受HEX值?
XXXX-XXXX,x是在0-9,AF,AF
所以我添加了一个MaskedTextBox中并尝试编辑其面膜属性,但没有成功。
一类新的需要验证,如果输入值是有效和类类型应设置为MaskedTextBox1的ValidatingType属性如下:
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);
}
}
您不能使用MaskedTextBox
开箱即用。如果你看看在the documentation for the Mask
property中详细描述的允许掩码元素,你会发现你没有办法只能执行十六进制输入。
虽然您可以使用NumericUpDown
并将其Hexadecimal
属性设置为true
。