| | |
Textboxes
Please support our C# advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Feb 2009
Posts: 1
Reputation:
Solved Threads: 0
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*
- 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
B.Sc Computer Science, Helwan University
Technical blog | http://ramymahrous.wordpress.com
LinkedIn | http://www.linkedin.com/in/ramymahrous
•
•
•
•
Ddanbe has given the correct answer. When the key is pressed if you dont want it, you can do something about it then.
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)
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>
•
•
Join Date: Jan 2009
Posts: 32
Reputation:
Solved Threads: 1
•
•
•
•
<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>
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.
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
Make love, no war. Cave ab homine unius libri.
Danny
![]() |
Similar Threads
- MultiLine TextBoxes in GridView edit mode (ASP.NET)
- C# TextBoxes (ASP.NET)
- How to databind to textboxes (ASP.NET)
- Browser not displaying Textboxes!! (ASP.NET)
- Ref. 20 textboxes on a form numerically (VB.NET)
- Validate textboxes (HTML and CSS)
- dynamically generated textboxes & radio buttons php, insert into db (PHP)
- Data Navigation in textboxes with ADO.NET (like ADO) (ASP.NET)
Other Threads in the C# Forum
- Previous Thread: Array syntax issue
- Next Thread: toolstrip menu icon
Views: 1085 | Replies: 10
| Thread Tools | Search this Thread |
Tag cloud for C#
.net access ado.net algorithm array barchart bitmap box broadcast button buttons c# chat check checkbox class client color combobox control conversion csharp custom database datagrid datagridview dataset datetime degrees development drawing encryption enum event excel file files form format ftp function gcd gdi+ http httpwebrequest image index input java list listbox listener login mandelbrot math mouseclick mysql networking object oracle path photoshop picturebox pixelinversion post prime programming radians regex remote remoting resource richtextbox save saving serialization server sleep socket sql statistics stream string table tcp text textbox thread time timer treeview update usercontrol validation view webbrowser windows winforms wpf xml






