VB. NET enables only numbers to be entered in the text box

Source: Internet
Author: User

When compiling a computing program, the text box is generally used to input digital data. In this case, you can follow these steps to ensure the correctness of user input data: first, add the Textbox Control and change the imemode attribute value to disable, so that the Chinese input method cannot be used in the text box. Then

Keypress

Add the following code for the keypress event in the text box

Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Textbox1.KeyPressIf Char.IsDigit(e.KeyChar) Or e.KeyChar = Chr(8) Thene.Handled = FalseElsee.Handled = TrueEnd IfEnd Sub

E. keychar is the character corresponding to the key entered by the keyboard. The isdigit function can determine whether it is a number ranging from 0 to 9, and CHR (8) is the return key. When E. when the value of handled is true, the program determines that the keypress event has been processed, and the content of the text box will not change. It is not difficult to see from the above program that the text box can only accept numbers and the Return key, that is, this text box is used to input integers.

Decimal point

If you need to enter a decimal point in the text, you must be able to enter the decimal point "." And the decimal point can only be entered once. In this case, you can change the above function to the following format:

Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Textbox1.KeyPressIf Char.IsDigit(e.KeyChar) or e.KeyChar = Chr(8) or e.KeyChar = "." ThenIf e.KeyChar = "." And InStr(TextBox1.Text, ".") > 0 Thene.Handled = TrueElsee.Handled = FalseEnd IfElsee.Handled = TrueEnd IfEnd Sub

The instr function returns the position of the specified character in the string. If the specified character is not included in the string, a negative number is returned. The text box can be used to enter positive integers and decimals.

Negative

If you need to enter a negative number in the text, you must be able to enter a negative number "-", and can only be the first character entered in the text box. At this time, you can change the above function to the following format:

Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Textbox1.KeyPressIf Char.IsDigit(e.KeyChar) Or e.KeyChar = "." Or e.KeyChar = Chr(8) ThenIf e.KeyChar = "." And InStr(TextBox1.Text, ".") > 0 Thene.Handled = TrueElsee.Handled = FalseEnd IfElseIf e.KeyChar = "-" And TextBox1.Text = "" Thene.Handled = FalseElsee.Handled = TrueEnd IfEnd Sub

The text box can be used to enter positive or negative integers and decimals.

Batch Processing

When there are multiple text boxes that require input restrictions, you can add keypress events for multiple text boxes after the preceding code handles. Each event is separated by commas (,). For example

Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles Textbox1.KeyPress,Textbox2.KeyPress,Textbox3.KeyPress

To determine the content of a text box, you must modify the code so that it can process multiple text boxes at the same time. That is, you can change textbox1.text in the last two sections to ctype (sender, textbox ). text, so that you can access the seat. For example, you can change the code that can enter a negative number:

Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) HandlesTextbox1.KeyPress,Textbox2.KeyPress,Textbox3.KeyPressIf Char.IsDigit(e.KeyChar) Or e.KeyChar = "." Or e.KeyChar = Chr(8) ThenIf e.KeyChar = "." And InStr(CType(sender, TextBox).Text, ".") > 0 Thene.Handled = TrueElsee.Handled = FalseEnd IfElseIf e.KeyChar = "-" And CType(sender, TextBox).Text = "" Thene.Handled = FalseElsee.Handled = TrueEnd IfEnd Sub


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.