[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Help with a 'numeric-only' sub

Posted on 2006-07-11
4
Medium Priority
?
198 Views
Last Modified: 2010-04-23
Found this on another forum and it work 95% for me.
The 'problem' that I have is when data is input with a decimal amount (9.50), I can no longer backspace to clear out the decimal portion (I can highlight the two decimal digits and delete them that way).  Can anyone help better this for me?
I honestly don't know what's going on here... :(

    Private Sub NumericKeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
        Dim tb As TextBox = CType(sender, TextBox)
        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
    End Sub
0
Comment
Question by:sirbounty
  • 2
4 Comments
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 800 total points
ID: 17086018
Change you If statements to:

    If Not (Char.IsDigit(e.KeyChar) Or Char.IsControl(e.KeyChar) Or (e.KeyChar = "." And tb.Text.IndexOf(".") < 0)) And e.KeyChar <> 8 Then

And:

    If (tb.SelectionStart > tb.Text.Length - 2 And tb.Text.IndexOf(".") >= 0 And tb.Text.IndexOf(".") + 3 = tb.Text.Length) And e.KeyChar <> 8 Then
0
 
LVL 67

Author Comment

by:sirbounty
ID: 17086193
I get:

Operator '<>' is not defined for types 'Char' and 'Integer'
0
 
LVL 96

Assisted Solution

by:Bob Learned
Bob Learned earned 200 total points
ID: 17086472
e.KeyChar <> ChrW(8)

Bob
0
 
LVL 67

Author Comment

by:sirbounty
ID: 17086476
I got it working with just changing this one line:

If (tb.SelectionStart > tb.Text.Length - 2 And tb.Text.IndexOf(".") >= 0 And tb.Text.IndexOf(".") + 3 = tb.Text.Length) And e.KeyChar <> ControlChars.Back Then
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Loops Section Overview
Suggested Courses

830 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