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)
- Public class inttextbox: system. Windows. Forms. textbox
- {
- Private int selectpos = 0;
- Public inttextbox ()
- : Base ()
- {
- This. backcolor = color. Beige;
- This. textchanged + = new eventhandler (this. textchage );
- This. Leave + = new eventhandler (this. focusleave );
- }
- // When the focus changes, you can choose one as needed.
- Public void focusleave (Object sender, system. eventargs E)
- {
- If (this. Text! = "")
- {
- This. Text = todbc (this. Text );
- If (! (New RegEx (@ "^ -? /D + $ "). ismatch (this. Text ))
- {
- This. backcolor = color. orangered;
- MessageBox. Show ("the input content is invalid! "," Input prompt ");
- This. Focus ();
- }
- Else
- {
- This. backcolor = color. Beige;
- }
- }
- }
- // When the content changes
- Public void textchage (Object sender, system. eventargs E)
- {
- Selectpos = This. selectionstart;
- If (this. Text! = "")
- {
- // Zhengzhou expression is used here
- If (! (New RegEx (@ "^ -? /D + $ "). ismatch (this. Text ))
- {
- This. backcolor = color. orangered;
- This. selectionstart = selectpos;
- }
- Else
- {
- This. backcolor = color. Beige;
- This. selectionstart = selectpos;
- }
- }
- Else
- {
- This. backcolor = color. Beige;
- }
- }
- // Convert the fullwidth to halfwidth. You can also select this function.
- Public String todbc (string input)
- {
- Char [] C = input. tochararray ();
- For (INT I = 0; I <C. length; I ++)
- {
- If (C [I] = 12288)
- {
- C [I] = (char) 32;
- Continue;
- }
- If (C [I]> 65280 & C [I] <65375)
- C [I] = (char) (C [I]-65248 );
- }
- Return new string (C );
- }
- }
The following code can only be used to enter floating-point numbers (similar to other Chinese characters, letters, and so on)
- Public class floattextbox: system. Windows. Forms. textbox
- {
- Private int selectpos = 0;
- Public floattextbox ()
- : Base ()
- {
- This. backcolor = color. Beige;
- This. textchanged + = new eventhandler (this. textchage );
- This. Leave + = new eventhandler (this. focusleave );
- }
- // When the focus changes
- Public void focusleave (Object sender, system. eventargs E)
- {
- If (this. Text! = "")
- {
- This. Text = todbc (this. Text );
- If (! (New RegEx (@ "^/d + (/./d + )? $ "). Ismatch (this. Text ))
- {
- This. backcolor = color. orangered;
- MessageBox. Show ("the input content is invalid! "," Input prompt ");
- This. Focus ();
- }
- Else
- {
- This. backcolor = color. Beige;
- }
- }
- }
- // When the content changes
- Public void textchage (Object sender, system. eventargs E)
- {
- Selectpos = This. selectionstart;
- If (this. Text! = "")
- {
- If (! (New RegEx (@ "^/d + (/./d + )? $ "). Ismatch (this. Text ))
- {
- This. backcolor = color. orangered;
- This. selectionstart = selectpos;
- }
- Else
- {
- This. backcolor = color. Beige;
- This. selectionstart = selectpos;
- }
- }
- Else
- {
- This. backcolor = color. Beige;
- }
- }
- // Convert the fullwidth to halfwidth
- Public String todbc (string input)
- {
- Char [] C = input. tochararray ();
- For (INT I = 0; I <C. length; I ++)
- {
- If (C [I] = 12288)
- {
- C [I] = (char) 32;
- Continue;
- }
- If (C [I]> 65280 & C [I] <65375)
- C [I] = (char) (C [I]-65248 );
- }
- Return new string (C );
- }
- }