• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 246
  • Last Modified:

How can I capture and prevent user from enter special charaters like *, @, and comma in KeyDown event?

What I have below is not working currently.  It doesn't capture the '*' char.  Also, how can I capture comma(,) and period(.)?  Thank you.
        private void txtContextName_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Decimal || e.KeyCode == (Keys.Shift & Keys.D8) ||
                e.KeyCode == (Keys.Shift & Keys.D1))
                e.Handled = true;
            //e.KeyCode == Keys. '@', '.', ',', '!','*' }
        }
0
lapucca
Asked:
lapucca
1 Solution
 
Fernando SotoRetiredCommented:
Hi lapucca;

Use the KeyPressed event.

        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == '@' || e.KeyChar == ',' || e.KeyChar == '*')
            {
                e.Handled = true;
            }
        }

Fernando
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now