Home > Software design >  Blocking Underscore Input in TextBox
Blocking Underscore Input in TextBox

Time:07-23

Blocking Underscore Input in TextBox

Hello All,

I'm trying to block underscore (_) from being entered into a TextBox but I'm having no luck:

private void CoupontextBox_KeyPress(object sender, KeyPressEventArgs e)
{
    if (char.IsSymbol('_'))
    {
        e.Handled = true;
    }
}

Thanks in advance!

CodePudding user response:

It seems that you have just a typo:

// if '_' character is a symbol? - no; false will be returned
char.IsSymbol('_')

is always false. You should put something like this

private void CoupontextBox_KeyPress(object sender, KeyPressEventArgs e)
{
    // if character that corresponds key pressed is '_' we handle the message 
    if (e.KeyChar == '_')
    {
        e.Handled = true;
    }
}
  •  Tags:  
  • c#
  • Related