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

Posted on 2007-10-12
Last Modified: 2013-12-17
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. '@', '.', ',', '!','*' }
Question by:lapucca
    1 Comment
    LVL 62

    Accepted Solution

    Hi lapucca;

    Use the KeyPressed event.

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


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Is Threat Intelligence?

    Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

    Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (…
    In my previous article ( we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    11 Experts available now in Live!

    Get 1:1 Help Now