Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Prevent non-numeric input

Posted on 2006-07-11
4
Medium Priority
?
932 Views
Last Modified: 2012-06-27
I recall in VB6 that I could basically use the keydown/keypress event to test for the appropriate invalid characters and then just cancel the input (textbox).

What's the proper way for accomplishing this in VB.Net?
0
Comment
Question by:sirbounty
  • 2
  • 2
4 Comments
 
LVL 44

Accepted Solution

by:
bruintje earned 300 total points
ID: 17085615
Hi sirbounty,
----------

probably the same you can use the keypress like

Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
  If (IsNumeric(e.KeyChar) = False) Then e.Handled = True
End Sub

there is a masked textbox in vb 2005 but its limited to the mask you set [thats the purpose of course ;)]

----------
bruintje
share what you know, learn what you don't
0
 
LVL 67

Author Comment

by:sirbounty
ID: 17085636
Yeah - I've got 2005 at work, but doing this in 2003. :(

Okay, so this will be currency data, can I allow for a decimal as well?
0
 
LVL 67

Author Comment

by:sirbounty
ID: 17085655
Incidentally, I found this on another forum...not too bad..
Private Sub NumericKeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
Dim tb As TextBox = sender
        If Not (Char.IsDigit(e.KeyChar) Or Char.IsControl(e.KeyChar) Or (e.KeyChar = "." And tb.Text.IndexOf(".") < 0)) Then
            e.Handled = True
        End If
        If (tb.SelectionStart > tb.Text.Length - 2 And tb.Text.IndexOf(".") >= 0 And tb.Text.IndexOf(".") + 3 = tb.Text.Length) Then
            e.Handled = True
        End If
        If e.Handled = False And tb.Text.Length = 0 Then
            tb.Text = "$" + e.KeyChar
            e.Handled = True
            tb.SelectionStart = 2
        End If
End Sub
0
 
LVL 44

Expert Comment

by:bruintje
ID: 17085705
well, then you solved your own question :-)
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Loops Section Overview

926 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question