textbox limitation ?

Please support our C# advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Jul 2009
Posts: 13
Reputation: Dum_ is an unknown quantity at this point 
Solved Threads: 0
Dum_ Dum_ is offline Offline
Newbie Poster

textbox limitation ?

 
0
  #1
21 Days Ago
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 .
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 1,909
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: 274
ddanbe's Avatar
ddanbe ddanbe is offline Offline
Posting Virtuoso
 
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.
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 2009
Posts: 318
Reputation: Ryshad has a spectacular aura about Ryshad has a spectacular aura about 
Solved Threads: 57
Ryshad's Avatar
Ryshad Ryshad is offline Offline
Posting Whiz
 
1
  #3
21 Days Ago
One way would be to limit the keys the user can press:
  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
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
Reply With Quote Quick reply to this message  
Join Date: Jul 2009
Posts: 13
Reputation: Dum_ is an unknown quantity at this point 
Solved Threads: 0
Dum_ Dum_ is offline Offline
Newbie Poster

solved

 
0
  #4
21 Days Ago
I combined the code and it's working . Tnx
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 318
Reputation: Ryshad has a spectacular aura about Ryshad has a spectacular aura about 
Solved Threads: 57
Ryshad's Avatar
Ryshad Ryshad is offline Offline
Posting Whiz
 
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
Reply With Quote Quick reply to this message  
Reply

Tags
input, textbox, validation

This thread has been marked solved.
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