When the most program developers create one control on a form which to operate number such as Quantity of Item, Number of item.., always set that control use only number to help user avoid using wrong input. In this application, I add one TextBox(Name: TextBoxOnlyNumber; Text: Only Number) and use this textbox with KeyPress_Event(When we type text in textbox, the code will execute) then message box will show("Please enter numbers only"), if we input not number.
Code
Public Class OnlyNumber
Private Sub TextBoxOnlyNumber_Click(sender As Object, e As EventArgs) Handles
TextBoxOnlyNumber.Click
TextBoxOnlyNumber.Text = ""
End Sub
Private Sub TextBoxOnlyNumber_MouseLeave(sender As Object, e As EventArgs) Handles
TextBoxOnlyNumber.MouseLeave
TextBoxOnlyNumber.Text = "Only Number!"
End Sub
Private Sub TextBoxOnlyNumber_KeyPress(sender As Object, e As KeyPressEventArgs) Handles
TextBoxOnlyNumber.KeyPress
If Asc(e.KeyChar) <> 13 AndAlso Asc(e.KeyChar) <> 8 AndAlso Not IsNumeric(e.KeyChar) Then
MessageBox.Show("Please enter
numbers only")
e.Handled = True
End If
End Sub
End Class
this code, there are 3 block,our main code is only one block(TextBoxOnlyNumber_KeyPress), but I added another 2 blocks are:
- TextBoxOnlyNumber_Click : For clear textbox when we click on textbox.
- TextBoxOnlyNumber_MouseLeave : for showing text("Only Number") when the mouse leave from textbox.
0 comments:
Post a Comment