IT KNOWLEDGE

IT KNOWLEDGE
BC

Saturday, May 24, 2014

How to set input number only in textbox (VB.Net)

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: TextBoxOnlyNumberText: 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:
  1. TextBoxOnlyNumber_Click : For clear textbox when we click on textbox.
  2. TextBoxOnlyNumber_MouseLeave : for showing text("Only Number") when the mouse leave from textbox.

Please Give Us Your 1 Minute In Sharing This Post!
SOCIALIZE IT →
FOLLOW US →
SHARE IT →
Powered By: itech-9999

0 comments: