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