Sei sulla pagina 1di 3

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 de cimal 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 Other 1 submission(s) by this author

Report Bad Submission Use this form to notify 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 1/31/2003 1:03:38 PM: Your way to verify the values is far more elegant than mine. Well done! 3/25/2003 7:47:55 PM: Good, simple code. There's a few implicit conversions that cause problems when Option Explicit is O N. You should correct your code accordingly. 7/6/2003 1:47:18 AM: crouchie1998 This code isn't anything new. Its documented in a few VB.NET Books. However, thi s code isn't in a user control, whereas the code in the books is. 7/29/2003 9:25:49 PM: KeyPress doesn't stop a user from pasting an invalid value into the textbox! 8/7/2003 9:19:11 AM:

Tks it helped me ! =) 10/4/2003 9:39:12 AM: coder100 can't we just use the NumericTextBox control? 12/2/2003 2:43:16 PM: I thing you should use a Regular Expresion Validator. 1/7/2004 7:11:26 PM: IsNumeric(TextBox1.Text) Format(Val(textbox1.text), "###0.0") Should do the trick also 1/14/2004 1:33:18 AM: Tristan B. Astillero tnx for d feedback every1. will try to improve it wen i get 2 do some .net agen 2/9/2004 11:03:02 PM: pcito How about some good, old fashioned indentation? Otherwise, good code. 2/23/2004 4:32:17 AM: Tristan B. Astillero tnx for ur feedback every1. to answer some of the posts, this code was done by a beginner in .net, i didn't know they had it in books. it works only when typing in the values, NOT pasting it in. i'd only realized that i cn use it in a contr ol after i'd finished d proj(meaning, rush project). i tried indenting d code bu t it still came out like that. 4/20/2004 1:58:08 PM: CrZ This code does not work 'Handled' is not a member of 'System.EventArgs'.

8/30/2004 2:20:06 PM: Nigel Hooper How about making this code culturally aware by using the current Number Decimal Separator, instead of hard coding it? (see System.Globalization.CultureInfo.Curr entCulture.NumberFormat.NumberDecimalSeparator) or at least make the 8/30/2004 2:21:20 PM: Nigel Hooper or at least make the "." a constant / variable. 8/9/2005 6:25:25 AM: Sudha

Well Done. If more explanation could be given beginers will understand it better 3/31/2006 2:14:13 PM: Carlos Good Job my Friend 4/12/2007 1:22:04 AM: I love you... 1/29/2008 12:05:45 AM: williams i think that a simple method is already exist my friend.. 10/13/2010 3:59:40 PM: Cory S. Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows. Forms.KeyPressEventArgs) Handles TextBox1.KeyPress If e.KeyChar = "." Then Dim r = TextBox1.Text.IndexOf(".") If r > 0 Then e.KeyChar = "" Else e.KeyChar = "." End If ElseIf IsNumeric(e.KeyChar) = False Then e.KeyChar = "" End If End Sub another version... good beginner code. everybody that is leaving BS comments, re alize this is a website where a lot of beginners go to learn 'basic' programming . If you have a better way, show it.. commenting saying there is a better way wi th out showing anything doesnt help any body