Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4554964
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T17:09:32+00:00 2026-05-21T17:09:32+00:00

What code should I write to prevent any special characters except ‘_’ (underscore) while

  • 0

What code should I write to prevent any special characters except ‘_’ (underscore) while entering the name in text box?

If such character exist then a popup message should appear.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T17:09:33+00:00Added an answer on May 21, 2026 at 5:09 pm

    Rather than me writing the code for you, here are the basic steps required for accomplishing such a feat:

    1. Handle the KeyDown event for your TextBox control.

    2. Use something like the Char.IsSymbol method to verify whether or not the character that they typed is allowed. Make sure you check explicitly for the underscore, because you want to allow it as a special case of other symbols.

    3. If a valid character is typed, do nothing. WinForms will take care of inserting it into the textbox.

      However, if an invalid character is typed, you need to show the user a message, informing them that the character is not accepted by the textbox. A couple of things to do here:

      1. Set the e.SuppressKeyPress property to True. This will prevent the character from appearing in the textbox.

      2. Display a tooltip window on the textbox, indicating that the character the user typed is not accepted by the textbox and informing them what characters are considered valid input.
        The easiest way to do this is using the ToolTip class. Add this control to your form at design time, and display it when appropriate using one of the overloads of the Show method.
        In particular, you’ll want to use one of the overloads that allows you to specify an IWin32Window to associate the tooltip with (this is your textbox control).

        An example of a balloon-style tooltip, displaying an error message.

        Alternatively, instead of a tooltip, you can display a little error icon next to the textbox control, informing the user that their last input was invalid. This is easy to implement using an ErrorProvider control. Add it to your form at design time, just like the tooltip control, and call the SetError method at run-time to display an error message.

        An example of an ErrorProvider control, set on a text box.

        Whatever you do, do not display a message box! That disrupts the user trying to type, and it’s likely that they’ll inadvertently dismiss it by typing the next letter they wanted to type.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What criteria should I use to decide whether I write VBA code like this:
The following code should find the appropriate project tag and remove it from the
Where in code should conversions be done? client, server, business, or db? We currently
In this thread some one commented that the following code should only be used
I am pretty sure the following button-activated form code should raise a Control-F12 in
Do you guys think internationalization should only done at the code level or should
As Jeff Atwood asked : "What’s your logging philosophy? Should all code be littered
What should i use to code Classic ASP under Linux. I have really tried
Should I prepare my code for possible/predicted future changes so that it's easier to
Should I stick with Sun's Java code conventions for PHP code?

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.