Textboxes

Please support our C# advertiser: Intel Parallel Studio Home
Reply

Join Date: Feb 2009
Posts: 1
Reputation: C# beginner is an unknown quantity at this point 
Solved Threads: 0
C# beginner C# beginner is offline Offline
Newbie Poster

Textboxes

 
0
  #1
Feb 6th, 2009

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
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 2,065
Reputation: Ramy Mahrous is just really nice Ramy Mahrous is just really nice Ramy Mahrous is just really nice Ramy Mahrous is just really nice 
Solved Threads: 256
Featured Poster
Ramy Mahrous's Avatar
Ramy Mahrous Ramy Mahrous is offline Offline
Postaholic

Re: Textboxes

 
0
  #2
Feb 6th, 2009
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*
BI Developer | LINKdotNET
B.Sc Computer Science, Helwan University
Technical blog | http://ramymahrous.wordpress.com
LinkedIn | http://www.linkedin.com/in/ramymahrous
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 1,934
Reputation: ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of 
Solved Threads: 277
ddanbe's Avatar
ddanbe ddanbe is offline Offline
Posting Virtuoso

Re: Textboxes

 
0
  #3
Feb 6th, 2009
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.
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 46
Reputation: BlackSun is an unknown quantity at this point 
Solved Threads: 4
BlackSun BlackSun is offline Offline
Light Poster

Re: Textboxes

 
0
  #4
Feb 7th, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 1,735
Reputation: LizR has a spectacular aura about LizR has a spectacular aura about 
Solved Threads: 186
LizR LizR is offline Offline
Posting Virtuoso

Re: Textboxes

 
0
  #5
Feb 7th, 2009
Ddanbe has given the correct answer. When the key is pressed if you dont want it, you can do something about it then.
Did I just hear "You gotta help us, Doc. We've tried nothin' and we're all out of ideas" ? Is this you? Dont let this be you! I will put in as much effort as you seem to.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 324
Reputation: Diamonddrake is a jewel in the rough Diamonddrake is a jewel in the rough Diamonddrake is a jewel in the rough 
Solved Threads: 39
Diamonddrake's Avatar
Diamonddrake Diamonddrake is offline Offline
Posting Whiz

Re: Textboxes

 
0
  #6
Feb 7th, 2009
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
  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>
Reply With Quote Quick reply to this message  
Join Date: Jan 2009
Posts: 32
Reputation: subtercosm is an unknown quantity at this point 
Solved Threads: 1
subtercosm subtercosm is offline Offline
Light Poster

Re: Textboxes

 
0
  #7
Feb 7th, 2009
Originally Posted by Diamonddrake View Post
<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?
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 1,934
Reputation: ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of 
Solved Threads: 277
ddanbe's Avatar
ddanbe ddanbe is offline Offline
Posting Virtuoso

Re: Textboxes

 
0
  #8
Feb 8th, 2009
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.
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 1,735
Reputation: LizR has a spectacular aura about LizR has a spectacular aura about 
Solved Threads: 186
LizR LizR is offline Offline
Posting Virtuoso

Re: Textboxes

 
0
  #9
Feb 8th, 2009
Not all keys appear in a keypress, but they do in the keydown/up
Did I just hear "You gotta help us, Doc. We've tried nothin' and we're all out of ideas" ? Is this you? Dont let this be you! I will put in as much effort as you seem to.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 1,934
Reputation: ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of ddanbe has much to be proud of 
Solved Threads: 277
ddanbe's Avatar
ddanbe ddanbe is offline Offline
Posting Virtuoso

Re: Textboxes

 
0
  #10
Feb 8th, 2009
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.
Today is a gift, that's why it is called "The Present".
Make love, no war. Cave ab homine unius libri.
Danny
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC