954,514 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Textboxes

:-/
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

C# beginner
Newbie Poster
1 post since Feb 2009
Reputation Points: 10
Solved Threads: 0
 

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*

Ramy Mahrous
Postaholic
2,196 posts since Aug 2006
Reputation Points: 480
Solved Threads: 276
 

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.

ddanbe
Senior Poster
3,829 posts since Oct 2008
Reputation Points: 2,070
Solved Threads: 661
 

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

BlackSun
Light Poster
46 posts since Feb 2008
Reputation Points: 28
Solved Threads: 5
 

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

LizR
Posting Virtuoso
1,791 posts since Aug 2008
Reputation Points: 196
Solved Threads: 190
 
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.


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.

Diamonddrake
Master Poster
724 posts since Mar 2008
Reputation Points: 442
Solved Threads: 89
 
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.

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

subtercosm
Light Poster
34 posts since Jan 2009
Reputation Points: 10
Solved Threads: 1
 

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.

ddanbe
Senior Poster
3,829 posts since Oct 2008
Reputation Points: 2,070
Solved Threads: 661
 

Not all keys appear in a keypress, but they do in the keydown/up

LizR
Posting Virtuoso
1,791 posts since Aug 2008
Reputation Points: 196
Solved Threads: 190
 

You could also check out this snippet :
http://www.daniweb.com/code/snippet1094.html
Here only "numeric" input is allowed, even the input of only one decimal point is taken into account.
It is the opposite of what you want, but it seems to me that it is easy to change that into something you want.

ddanbe
Senior Poster
3,829 posts since Oct 2008
Reputation Points: 2,070
Solved Threads: 661
 

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.

Diamonddrake
Master Poster
724 posts since Mar 2008
Reputation Points: 442
Solved Threads: 89
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You