One simple way is to use
Control.Validating Event (System.Windows.Forms)[
^]. In this event check that only characters allowed in hex values are present. If any extra characters are included, set e.Cancel to true.
Something like:
private void textBox1_Validating(object sender, CancelEventArgs e)
{
char[] allowedChars = new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
foreach (char character in textBox1.Text.ToUpper().ToArray())
{
if (!allowedChars.Contains(character))
{
System.Windows.Forms.MessageBox.Show(string.Format("'{0}' is not a hexadecimal character", character));
e.Cancel = true;
}
}
}