How can we make textbox only contain numbers, Chinese characters, and letters?

Source: Internet
Author: User

In programming, textbox is often used to accept only numbers (or others, such as Chinese characters, letters, and so on ), at this time, we may need to re-encapsulate a Textbox (other methods can also be used). We often see someone asking this question. Today, I took some time to encapsulate it. Now I share it with you, I hope to provide some help to you. If there is anything wrong, please make an axe. In addition, if you need to reprint it, please indicate the source:
Half smoke Ajie. Http://blog.csdn.net/gisfarmer/ thanks.
Only code with numbers (INT type) can be entered. (copy the Code directly)

  1. Public class inttextbox: system. Windows. Forms. textbox
  2. {
  3. Private int selectpos = 0;
  4. Public inttextbox ()
  5. : Base ()
  6. {
  7. This. backcolor = color. Beige;
  8. This. textchanged + = new eventhandler (this. textchage );
  9. This. Leave + = new eventhandler (this. focusleave );
  10. }
  11. // When the focus changes, you can choose one as needed.
  12. Public void focusleave (Object sender, system. eventargs E)
  13. {
  14. If (this. Text! = "")
  15. {
  16. This. Text = todbc (this. Text );
  17. If (! (New RegEx (@ "^ -? /D + $ "). ismatch (this. Text ))
  18. {
  19. This. backcolor = color. orangered;
  20. MessageBox. Show ("the input content is invalid! "," Input prompt ");
  21. This. Focus ();
  22. }
  23. Else
  24. {
  25. This. backcolor = color. Beige;
  26. }
  27. }
  28. }
  29. // When the content changes
  30. Public void textchage (Object sender, system. eventargs E)
  31. {
  32. Selectpos = This. selectionstart;
  33. If (this. Text! = "")
  34. {
  35. // Zhengzhou expression is used here
  36. If (! (New RegEx (@ "^ -? /D + $ "). ismatch (this. Text ))
  37. {
  38. This. backcolor = color. orangered;
  39. This. selectionstart = selectpos;
  40. }
  41. Else
  42. {
  43. This. backcolor = color. Beige;
  44. This. selectionstart = selectpos;
  45. }
  46. }
  47. Else
  48. {
  49. This. backcolor = color. Beige;
  50. }
  51. }
  52. // Convert the fullwidth to halfwidth. You can also select this function.
  53. Public String todbc (string input)
  54. {
  55. Char [] C = input. tochararray ();
  56. For (INT I = 0; I <C. length; I ++)
  57. {
  58. If (C [I] = 12288)
  59. {
  60. C [I] = (char) 32;
  61. Continue;
  62. }
  63. If (C [I]> 65280 & C [I] <65375)
  64. C [I] = (char) (C [I]-65248 );
  65. }
  66. Return new string (C );
  67. }
  68. }

The following code can only be used to enter floating-point numbers (similar to other Chinese characters, letters, and so on)

  1. Public class floattextbox: system. Windows. Forms. textbox
  2. {
  3. Private int selectpos = 0;
  4. Public floattextbox ()
  5. : Base ()
  6. {
  7. This. backcolor = color. Beige;
  8. This. textchanged + = new eventhandler (this. textchage );
  9. This. Leave + = new eventhandler (this. focusleave );
  10. }
  11. // When the focus changes
  12. Public void focusleave (Object sender, system. eventargs E)
  13. {
  14. If (this. Text! = "")
  15. {
  16. This. Text = todbc (this. Text );
  17. If (! (New RegEx (@ "^/d + (/./d + )? $ "). Ismatch (this. Text ))
  18. {
  19. This. backcolor = color. orangered;
  20. MessageBox. Show ("the input content is invalid! "," Input prompt ");
  21. This. Focus ();
  22. }
  23. Else
  24. {
  25. This. backcolor = color. Beige;
  26. }
  27. }
  28. }
  29. // When the content changes
  30. Public void textchage (Object sender, system. eventargs E)
  31. {
  32. Selectpos = This. selectionstart;
  33. If (this. Text! = "")
  34. {
  35. If (! (New RegEx (@ "^/d + (/./d + )? $ "). Ismatch (this. Text ))
  36. {
  37. This. backcolor = color. orangered;
  38. This. selectionstart = selectpos;
  39. }
  40. Else
  41. {
  42. This. backcolor = color. Beige;
  43. This. selectionstart = selectpos;
  44. }
  45. }
  46. Else
  47. {
  48. This. backcolor = color. Beige;
  49. }
  50. }
  51. // Convert the fullwidth to halfwidth
  52. Public String todbc (string input)
  53. {
  54. Char [] C = input. tochararray ();
  55. For (INT I = 0; I <C. length; I ++)
  56. {
  57. If (C [I] = 12288)
  58. {
  59. C [I] = (char) 32;
  60. Continue;
  61. }
  62. If (C [I]> 65280 & C [I] <65375)
  63. C [I] = (char) (C [I]-65248 );
  64. }
  65. Return new string (C );
  66. }
  67. }

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.