VB icon

Accept numeric values only in a textbox

Email
Submitted on: 1/1/2015 3:32:00 PM
By: Tristan B. Astillero (from psc cd)  
Level: Intermediate
User Rating: By 22 Users
Compatibility: VB.NET
Views: 4695
 
     It validates the entries of a textbox while it is being entered, and allows only one instance of the decimal point(period) to be entered. Please vote and leave a comment so that at least i know im doing something right. =)
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
//**************************************
// Name: Accept numeric values only in a textbox
// Description:It validates the entries of a textbox while it is being entered, and allows only one instance of the decimal point(period) to be entered. Please vote and leave a comment so that at least i know im doing something right. =)
// By: Tristan B. Astillero (from psc cd)
//
// Inputs:It has 2 parameters: e.keychar(eventargs) and the textbox control.
//
// Returns:Returns a boolean value indicating if the event is handled or not.
//
// Assumes:If you have any suggestions or encounter an error I would appreciate your comments. Thanks. I it's simple but hopefully this will garner me some votes. =P
//
// Side Effects:None to my knowledge(so far).
//**************************************

Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
 e.Handled = NumbersOnly(e.KeyChar, TextBox1)
 End Sub
Private Function NumbersOnly(ByVal pstrChar As Char, ByVal oTextBox As TextBox) As Boolean
 'validate the entry for a textbox limiting it to only numeric values and the decimal point
 If (Convert.ToString(pstrChar) = "." And InStr(oTextBox.Text, ".")) Then Return True 'accept only one instance of the decimal point
 If Convert.ToString(pstrChar) <> "." And pstrChar <> vbBack Then
 Return IIf(IsNumeric(pstrChar), False, True) 'check if numeric is returned
 End If
 Return False 'for backspace
 End Function


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Intermediate category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments

3/17/2016 8:06:00 AMBassem Yehia

Very Good Function Thanks for Sharing
(If this comment was disrespectful, please report it.)

 
4/30/2017 6:52:56 PMnikhil

how to add mp3 or mp4 file in a gridviewcontrol and download the file also ...can any one post a source code
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.