:-/
Hello,

I am trying to allow only text in a textbox, not numbers or symbols, and when i user does enter something other than that it diplays a message box that says only letters please. I know how to do a messagebox I just can't seem to get on how i write the code to allow only letters.

HElp,
C# beginner

In textbox ChangedText event handler you can check on the character the user enters, Char.IsSymbol, Char.IsDigital, you'll make use of Char class.
- If you searched in this forum you'll find a lot of threads talk about that too..
*Show us your effort*

Check the KeyEventArgs in your TextBox KeyDown event handler.
Don't annoy a user with an error messagebox, simlpy do nothing when a user types in anything other then letters.

You can use "System.Text.RegularExpressions.Regex" class to specify the text format or u can chech the rang of ascii charcters numbers thats will be writen in ur textbox if not send ur exception error

Ddanbe has given the correct answer. When the key is pressed if you dont want it, you can do something about it then.

Ddanbe has given the correct answer. When the key is pressed if you dont want it, you can do something about it then.

That would be "a" correct answer, If in program you achieve your the desired effect, it is correct. there is no "right" way to program.

although that would be one of the best ways to go about it in this programmers opinion.

one thing to remember, if you desire to use the ever popular solution to simply ask the system, if a the key is a particular character, don't do anything. its usually necessary to subscribe to the "keypress" event. not the "keyup" or "keydown" events. I ran into that problem when i first got started, some one on a forum not unlike this one helped me out because i was using the "keyup" event, and it would fail to handle the event entirely. the code would execute, but the text still appeared in the text box.

Google is your friend.
here is an example of a custom text box control that only allows digits

public class NumbersOnlyTextBox : TextBox
{
  public NumbersOnlyTextBox()
  {
    KeyPress += new KeyPressEventHandler( HandleKeyPress );
  }

  private void HandleKeyPress( object sender, KeyPressEventArgs e )
  {
    if ( !( char.IsDigit( e.KeyChar ) || char.IsControl( e.KeyChar ) ) )
      e.Handled = true;
  }
}

its not necessary to use a custom control, you could simply just do the same on your form directly.

<rant>
it can't be stressed enough people. I think it should be a rule.
Google before thread!
I say all forums should set up to check for a google cookie, if it isn't there, then the submit button on the new thread page automatically opens a new browser window with the search results in google of whatever was entered in the subject text box of the forum.
</rant>

<rant>
it can't be stressed enough people. I think it should be a rule.
Google before thread!
I say all forums should set up to check for a google cookie, if it isn't there, then the submit button on the new thread page automatically opens a new browser window with the search results in google of whatever was entered in the subject text box of the forum.
</rant>

What about people who use other search engines? How would you recommend detecting this cookie?

The order in which the Key event are fired is :
KeyDown
KeyPress
KeyUp
Any filtering of chars should be done in KeyDown or KeyPress events.

as for "people who use other search engines"
it was just an idea, if could easily be done using browser history, or anything like that. It was more of a serious tone joke.

and as for using the keydown event.
even as shown in ddanbe's example, handling the entire key event can only be done in a keypress event! his example checks durring the keydown event but the keypress event actually handles the event and keeps the text box from receiving an input, that was my whole point. anyone that needs to ask this question in a forum needs that kind of information. if they don't know how to do, then they probably don't know the specifics such as that.

further more great snippet ddanbe.

This article has been dead for over six months. Start a new discussion instead.