| | |
Textboxes
Please support our C# advertiser: Intel Parallel Studio Home
![]() |
•
•
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
| Thread Tools | Search this Thread |
.net access algorithm alignment app array barchart bitmap box broadcast buttons c# c#gridviewcolumn check checkbox client combobox communication control conversion csharp custom database datagrid datagridview dataset datatable datetime degrees development draganddrop drawing elevated encryption enum event excel file focus form format forms function gdi+ hospitalmanagementsystem httpwebrequest image index input install java label list listbox login mandelbrot math messagebox mouseclick mysql operator path photoshop picturebox pixelinversion plotting pointer post programming radians read regex remote remoting richtextbox server sleep socket sql statistics stream string stringformatting sun table text textbox thread time timer update usercontrol validation visualstudio webbrowser whileloop windows winforms wpf xml






