Keyboard input control for. NET keypress events

Source: Internet
Author: User

Below is the code I implemented when using VB6.0


[Vb]
Private Sub txtCash_KeyPress (KeyAscii As Integer) 'limits the input amount to only numbers
If KeyAscii <48 Or KeyAscii> 57 Then
KeyAscii = 0
MsgBox "only numbers can be entered !, Enter again! ", VbOKOnly + vbExclamation," warning"
TxtCash. SetFocus
End If
End Sub

Private Sub txtCash_KeyPress (KeyAscii As Integer) 'limits the input amount to only numbers
If KeyAscii <48 Or KeyAscii> 57 Then
KeyAscii = 0
MsgBox "only numbers can be entered !, Enter again! ", VbOKOnly + vbExclamation," warning"
TxtCash. SetFocus
End If
End Sub

 

The following two methods can be used to solve this problem using VB. NET:
1. After the input, the system determines

After the input is controlled, all the data in the text box is input to determine whether all the entered data is numbers. The Code is as follows:


[Csharp]
If IsNumeric (txtCash. Text) = False Then
'Msgbox ("Please enter a valid number value in the amount ")
'Txtcash. Focus ()
'Txtcash. SelectAll ()
'Exit Sub
'End If

If IsNumeric (txtCash. Text) = False Then
'Msgbox ("Please enter a valid number value in the amount ")
'Txtcash. Focus ()
'Txtcash. SelectAll ()
'Exit Sub
'End If, but I think it will take a lot of time, because it is only known after the program runs, rather than knowing that the input is wrong.


2. Input judgment

I used this method, and I can judge it when I try again. The Code is as follows:


[Csharp]
Private Sub txtCash_KeyPress (sender As Object, e As KeyPressEventArgs) Handles txtCash. KeyPress
If Char. IsNumber (e. KeyChar) Or e. KeyChar = Chr (Keys. Back) then' should be allowed.
Return
Else
MsgBox ("enter a number ")
E. Handled = False
End If
E. Handled = True
End Sub

Private Sub txtCash_KeyPress (sender As Object, e As KeyPressEventArgs) Handles txtCash. KeyPress
If Char. IsNumber (e. KeyChar) Or e. KeyChar = Chr (Keys. Back) then' should be allowed.
Return
Else
MsgBox ("enter a number ")
E. Handled = False
End If
E. Handled = True
End Sub
E. keyChar is used to obtain the character corresponding to the key pressed by the keyboard, and keys. Back is the return key command.

E. Handled returns a boolean to indicate whether the event has been processed.


 

 

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.