Solved

Help with a 'numeric-only' sub

Posted on 2006-07-11
4
191 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 200 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 50 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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Receiving a string from a WebService Push 21 58
Show/Hide ASP.NET controls with JQuery best practices 3 85
How to limit User Input 2 27
Very Large data in MYSQL 7 73
This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

792 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