TextBox Control that can only enter numbers

Source: Internet
Author: User

 

[Instance description]


You can easily enter text information in the TextBox control, including letters, numbers, and Chinese characters.

If you need to enter the age information in the TextBox Control, the age information should only allow numbers. How can you restrict other information input by users?

This example shows how to only allow users to enter numbers in the TextBox Control. When other non-numeric buttons are pressed, the following prompt is displayed:

 

This example is very simple. Restrict the input and verify it directly in the KeyPress event. The following two methods are described:


[Key Technologies]
This example focuses on how to use the isDigit method of the Char structure to determine whether the character entered in the TextBox Control is a number, and use the Ascii value corresponding to the key to determine whether the key is within the specified range. The following describes the key technologies used in this instance in detail.

The KeyPress event of the TextBox Control contains two parameters: the sender event source and the e that contains the event data,

The KeyChar attribute of e can be used to obtain the keys that the user presses on the keyboard. The Handler attribute of e can be used to set whether the events of user buttons have been processed.
You can convert e. KeyChar to int to obtain the ASCII value corresponding to the key pressed by the current user.

(1) KeyChar attributes
This attribute is used to obtain or set the characters corresponding to the press key. The syntax format is as follows:

   KeyChar{ ; ; }

Parameter description
Attribute Value: Char structure, character corresponding to pressing the key.
(2) Handled attributes
This attribute is used to obtain or set a value. The value indicates whether the System. Windows. Forms. Control. KeyPress event has been processed. The syntax format is as follows:

   Handled{ ; ; }

Parameter description
Attribute Value: boolean type, only whether the KeyPress event has been processed.
The isDigit method of the Char structure can be used to determine whether the specified character is a decimal number, which is described in detail below.
(3) isDigit Method
This method only indicates whether a character belongs to the decimal number category. The syntax format is as follows:

    IsDigit( c)

Parameter description
C: One character
Return Value: return a Boolean value. If the character c is a part-time number, the return value is true. Otherwise, the return value is false.

Note: The KeyPressEventArgs Handled is set to true to cancel the KeyPress event. In this way, the user can cancel the value of pressing the button on the TextBox.

Design Process]
(1) Open Visual Studio, create a WinForm application, and name it OnlyDigit.
(2) Change the Name attribute of the default form Form1 to FrmMain and add a TextBox Control to the form to demonstrate that only numbers can be entered in the text box.
(3) The main code of the program is as follows:

    txt_Input_KeyPress(        (!.IsDigit(e.KeyChar))               MessageBox.Show(, , MessageBoxButtons.OK, MessageBoxIcon.Information);              e.Handled = ;     }

The code above indicates that the IsDigit method of char is used to determine whether the passed char character is a decimal number,

When you run this code, you will find that when we press a non-digit letter or symbol, it does achieve this effect,
However, when you press the press enter, exit, Ctrl, and so on, it is also considered that non-numbers are intercepted,

Therefore, we need to use the ASCII value to determine the Numerical range. Therefore, another method is as follows:

    txt_Input_KeyPress(        keyAsciiValue = Convert.ToInt32(e.KeyChar);                     (!(((keyAsciiValue >=  && keyAsciiValue <= ) || (keyAsciiValue ==  || keyAsciiValue ==       {              MessageBox.Show(, , MessageBoxButtons.OK, MessageBoxIcon.Information);              e.Handled =                                    }

 

I cannot insert attachments here. If you want source code, leave a message.

 

 

 

Related Article

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.