| | |
textbox limitation ?
Please support our C# advertiser: Intel Parallel Studio Home
Thread Solved |
0
#2 21 Days Ago
Maybe you can do something with this http://www.daniweb.com/code/snippet217084.html.
There are other related threads here but I think you are smart enough to search this site and find out for yourself.
There are other related threads here but I think you are smart enough to search this site and find out for yourself.
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
1
#3 21 Days Ago
One way would be to limit the keys the user can press:
This code will ensure that only numebrs from 1 to 25 can be ntered. However, there is a limitation to this solution; it will not prevent the user from PASTING invalid data into the textbox, it will only prevent them typing incorrect values.
Remember to mark the thread as solved if this has answered your question
C# Syntax (Toggle Plain Text)
private void TextBox_KeyPress(object sender, KeyPressEventArgs e) { //only allow numbers and control keys if (!char.IsDigit(e.KeyChar) && !char.IsControl(e.KeyChar)) { e.Handled = true; } else if (char.IsControl(e.KeyChar)) { e.Handled = false; } else { //using this rather than TextBox.Text.Length ensures //correct behaviour if user selects and overwrites contents of textbox int len = TextBox.Text.Length - TextBox.SelectionLength; //default to true then change to false if valid value entered e.Handled = true; switch (len) { //allow 1 or 2 as first character case 0: if (e.KeyChar == '1' || e.KeyChar == '2') e.Handled = false; break; //if this is the second character entered case 1: //check where user is typing. If user entered a number then moved the cursor back to start //of textbox then still only allow a 1 or 2. if (TextBox.SelectionStart == 0) { if (e.KeyChar >= '1' && e.KeyChar <= '2') e.Handled = false; } //if the user is entering the second digit then allow 0 to 5 else { if (e.KeyChar >= '0' && e.KeyChar <= '5') e.Handled = false; } break; default: break; } } }
This code will ensure that only numebrs from 1 to 25 can be ntered. However, there is a limitation to this solution; it will not prevent the user from PASTING invalid data into the textbox, it will only prevent them typing incorrect values.
Remember to mark the thread as solved if this has answered your question
Please don't take for granted the work that solvers do for you. Take the time to fully understand the code they give you so that you might adapt it to future problems.
"Learning is more than absorbing facts, it is acquiring understanding.” - William Arthur Ward
"Learning is more than absorbing facts, it is acquiring understanding.” - William Arthur Ward
0
#5 20 Days Ago
Glad it helped. Please mark the thread as solved
Please don't take for granted the work that solvers do for you. Take the time to fully understand the code they give you so that you might adapt it to future problems.
"Learning is more than absorbing facts, it is acquiring understanding.” - William Arthur Ward
"Learning is more than absorbing facts, it is acquiring understanding.” - William Arthur Ward
![]() |
Similar Threads
- Enable only one whitespace in textbox. (C#)
- Replace on pressing spacebar in textbox (C#)
- Focus on textBox? (C#)
- Code Snippet: Numbers-Only Textbox (C#)
- Code Snippet: Capturing numeric input in a TextBox (C#)
- Few Questions: textBox, checkBox/Form Related (C#)
Other Threads in the C# Forum
- Previous Thread: UDP Port Forwarding
- Next Thread: Dot in textbox.
| Thread Tools | Search this Thread |
action array asp asp.net basic box buttons c# c++ datagrid disabled editor email enabled enter fade file filter focus form i/o iframe input itunes javascript keyboard label linked list lists method mouse numeric online opacity output panel parsing php presence print python reading richtextbox save split string strings table text text-file textbox transparency trick trouble tuple user validation validator variables vb.net verify visual



. 


. Tnx 
