943,900 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Unsolved
  • Views: 1422
  • C# RSS
You are currently viewing page 1 of this multi-page discussion thread
Feb 6th, 2009
0

Textboxes

Expand Post »

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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
C# beginner is offline Offline
1 posts
since Feb 2009
Feb 6th, 2009
0

Re: Textboxes

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*
Featured Poster
Reputation Points: 480
Solved Threads: 276
Postaholic
Ramy Mahrous is offline Offline
2,189 posts
since Aug 2006
Feb 6th, 2009
0

Re: Textboxes

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.
Reputation Points: 2035
Solved Threads: 645
Senior Poster
ddanbe is offline Offline
3,740 posts
since Oct 2008
Feb 7th, 2009
0

Re: Textboxes

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
Reputation Points: 28
Solved Threads: 5
Light Poster
BlackSun is offline Offline
46 posts
since Feb 2008
Feb 7th, 2009
0

Re: Textboxes

Ddanbe has given the correct answer. When the key is pressed if you dont want it, you can do something about it then.
Reputation Points: 196
Solved Threads: 190
Posting Virtuoso
LizR is offline Offline
1,735 posts
since Aug 2008
Feb 7th, 2009
0

Re: Textboxes

Quote ...
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
C# Syntax (Toggle Plain Text)
  1. public class NumbersOnlyTextBox : TextBox
  2. {
  3. public NumbersOnlyTextBox()
  4. {
  5. KeyPress += new KeyPressEventHandler( HandleKeyPress );
  6. }
  7.  
  8. private void HandleKeyPress( object sender, KeyPressEventArgs e )
  9. {
  10. if ( !( char.IsDigit( e.KeyChar ) || char.IsControl( e.KeyChar ) ) )
  11. e.Handled = true;
  12. }
  13. }

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>
Reputation Points: 442
Solved Threads: 89
Master Poster
Diamonddrake is offline Offline
721 posts
since Mar 2008
Feb 7th, 2009
0

Re: Textboxes

<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?
Reputation Points: 10
Solved Threads: 1
Light Poster
subtercosm is offline Offline
32 posts
since Jan 2009
Feb 8th, 2009
0

Re: Textboxes

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.
Reputation Points: 2035
Solved Threads: 645
Senior Poster
ddanbe is offline Offline
3,740 posts
since Oct 2008
Feb 8th, 2009
0

Re: Textboxes

Not all keys appear in a keypress, but they do in the keydown/up
Reputation Points: 196
Solved Threads: 190
Posting Virtuoso
LizR is offline Offline
1,735 posts
since Aug 2008
Feb 8th, 2009
0

Re: Textboxes

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.
Reputation Points: 2035
Solved Threads: 645
Senior Poster
ddanbe is offline Offline
3,740 posts
since Oct 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: Array syntax issue
Next Thread in C# Forum Timeline: toolstrip menu icon





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC