Solved

Prevent non-numeric input

Posted on 2006-07-11
4
926 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 75 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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

746 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now