Solved

Help with a 'numeric-only' sub

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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video discusses moving either the default database or any database to a new volume.

757 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

21 Experts available now in Live!

Get 1:1 Help Now