Solved

Prevent non-numeric input

Posted on 2006-07-11
4
930 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses

635 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