When using hex numbers, you must prefix the number with 0x
. Your code currently compiles due to the coincidence that d
specifies the number is a double. The following code will correctly detect hyphens:
private void ContactNumTextBox_KeyPress(object sender, KeyPressEventArgs e)
{
const char hyphen = (char)0x2D;
const char delete = (char)0x08;
if (!char.IsNumber(e.KeyChar) && e.KeyChar != hyphen && e.KeyChar!= delete)
e.Handled = true;
}