In VB.net, only numbers can be entered in the text box.

Source: Internet
Author: User

In vb.net, only numbers can be entered in the text box.

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 add the following code to the keypress event in the text box

 

Private sub textbox1_keypress (byval sender as object, byval e as system. windows. forms. keypresseventargs) handles textbox1.keypress

If char. isdigit (e. keychar) or e. keychar = chr (8) then

E. handled = false

Else

E. handled = true

End if

End sub

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.keypress

If char. isdigit (e. keychar) or e. keychar = chr (8) or e. keychar = "." then

If e. keychar = "." and instr (textbox1.text, ".")> 0 then

E. handled = true

Else

E. handled = false

End if

Else

E. handled = true

End if

End sub

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.keypress

If char. isdigit (e. keychar) or e. keychar = "." or e. keychar = chr (8) then

If e. keychar = "." and instr (textbox1.text, ".")> 0 then

E. handled = true

Else

E. handled = false

End if

Elseif e. keychar = "-" and textbox1.text = "" then

E. handled = false

Else

E. handled = true

End if

End sub

  

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

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) handles textbox1.keypress, textbox2.keypress, textbox3.keypress

If char. isdigit (e. keychar) or e. keychar = "." or e. keychar = chr (8) then

If e. keychar = "." and instr (ctype (sender, textbox). text, ".")> 0 then

E. handled = true

Else

E. handled = false

End if

Elseif e. keychar = "-" and ctype (sender, textbox). text = "" then

E. handled = false

Else

E. handled = true

End if

  

End sub

 

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.