943,967 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Marked Solved
  • Views: 2985
  • C# RSS
Nov 4th, 2009
0

textbox limitation ?

Expand Post »
Hi,
i need to block input if first char 2 and second char must not be more 5. And first char input must be 1 or 2. Specifically I need the input of only 1-25 and nothing else. I tried and only allow entry of numbers, but fail to limit specified .
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Dum_ is offline Offline
13 posts
since Jul 2009
Nov 4th, 2009
0
Re: textbox limitation ?
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.
Reputation Points: 2035
Solved Threads: 645
Senior Poster
ddanbe is offline Offline
3,740 posts
since Oct 2008
Nov 4th, 2009
1
Re: textbox limitation ?
One way would be to limit the keys the user can press:
C# Syntax (Toggle Plain Text)
  1. private void TextBox_KeyPress(object sender, KeyPressEventArgs e)
  2. {
  3. //only allow numbers and control keys
  4. if (!char.IsDigit(e.KeyChar) && !char.IsControl(e.KeyChar))
  5. {
  6. e.Handled = true;
  7. }
  8. else if (char.IsControl(e.KeyChar))
  9. {
  10. e.Handled = false;
  11. }
  12. else
  13. {
  14. //using this rather than TextBox.Text.Length ensures
  15. //correct behaviour if user selects and overwrites contents of textbox
  16. int len = TextBox.Text.Length - TextBox.SelectionLength;
  17.  
  18. //default to true then change to false if valid value entered
  19. e.Handled = true;
  20. switch (len)
  21. {
  22.  
  23. //allow 1 or 2 as first character
  24. case 0:
  25. if (e.KeyChar == '1' || e.KeyChar == '2')
  26. e.Handled = false;
  27. break;
  28.  
  29. //if this is the second character entered
  30. case 1:
  31.  
  32. //check where user is typing. If user entered a number then moved the cursor back to start
  33. //of textbox then still only allow a 1 or 2.
  34. if (TextBox.SelectionStart == 0)
  35. {
  36. if (e.KeyChar >= '1' && e.KeyChar <= '2')
  37. e.Handled = false;
  38. }
  39. //if the user is entering the second digit then allow 0 to 5
  40. else
  41. {
  42. if (e.KeyChar >= '0' && e.KeyChar <= '5')
  43. e.Handled = false;
  44. }
  45. break;
  46.  
  47. default:
  48. break;
  49. }
  50. }
  51. }

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
Reputation Points: 512
Solved Threads: 246
Nearly a Posting Virtuoso
Ryshad is offline Offline
1,260 posts
since Aug 2009
Nov 4th, 2009
0

solved

I combined the code and it's working . Tnx
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Dum_ is offline Offline
13 posts
since Jul 2009
Nov 5th, 2009
0
Re: textbox limitation ?
Glad it helped. Please mark the thread as solved
Reputation Points: 512
Solved Threads: 246
Nearly a Posting Virtuoso
Ryshad is offline Offline
1,260 posts
since Aug 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

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: UDP Port Forwarding
Next Thread in C# Forum Timeline: Dot in textbox.





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


Follow us on Twitter


© 2011 DaniWeb® LLC