private bool CharAllowed(KeyPressEventArgs e) /* Check if char is allowed */
{
switch (e.KeyChar.ToString()) /* I used the string representation of the char to match case */
{
case "@":
return false;
default: /* These will be allowed symbols. Disallowed symbols should have their own case and return false as is above */
{
return true; /* Allowed values should return true */
}
}
}
private void TextBox1_KeyPress(object sender, KeyPressEventArgs e)
{
bool b = CharAllowed(e); /* Pass the event args of the Key press events and let the Char Allowed function decide if its a symbol you allow */
if (b) { Console.WriteLine(string.Concat(e.KeyChar.ToString(), " : Symbol allowed")); }
else { e.Handled = true; Console.WriteLine(string.Concat(e.KeyChar.ToString(), " : Symbol not allowed")); }
}