Solved

I need KeyPress code to limit textbox entry to 2 places of decimal

Posted on 2007-11-29
9
1,179 Views
Last Modified: 2013-11-26
Hi

I am using vb.net 2005 compact framework. I have the following keypress code that limits values entered into a textbox to numeric entry and back space. I need to take it one step further and limit entry to 2 places of decimals.

Code thus far:

Private Sub txtStandArea_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtStandArea.KeyPress
        If Not Char.IsDigit(e.KeyChar) And Not e.KeyChar = "." And Not e.KeyChar = ChrW(8) Then
            Beep()
            e.Handled = True
        End If
    End Sub

I was trying to incorporate txtStandArea.Text.Length >= txtStandArea.Text.IndexOf(".") + 2 into it but I just cant seem to get the proper way. Please help.

Thanks

PBLack
0
Comment
Question by:PBLack
9 Comments
 
LVL 7

Accepted Solution

by:
prosh0t earned 200 total points
ID: 20379150
It is actually fairly difficult to do something so simple because there are a lot of possibilities.  What if  someone pastes some text into the box that is invalid?  Then your code will never get called. What if the cursor is not all the way at the end of the text?  What if they try to enter two decimals into the box?

Basically the easiest way to implement this is to derive your own textbox control from the winforms text box, and store the data currently in the text box into a private variable.  Then, override an event that will trigger AFTER the text is already changed - for example, KeyUp or TextChanged.  Validate the text, and if it is an invalid decimal, set the text of your textbox back to what is in that private variable.  Otherwise, update your private variable with the new text.  

It sounds complicated but the code is actually fairly simple, and this is the best way to go about it because then you will be sure that you'll never have an invalid decimal in your textbox.  If you try to validate it one keystroke at a time, believe me the code gets messy.
0
 
LVL 70

Assisted Solution

by:Éric Moreau
Éric Moreau earned 150 total points
ID: 20379190
you better validate on lostfocus with a regular expression to be sure that the user does not paste a value. see http://www.emoreau.com/Entries/Articles/2003/03/Creating-your-own-Windows-Custom-Control.aspx
0
 
LVL 55

Assisted Solution

by:Jaime Olivares
Jaime Olivares earned 150 total points
ID: 20379197
also consider to use the MaskedTextBox control.
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

Author Comment

by:PBLack
ID: 20379277
I do not know if your concerns apply to compact framework. I came up with a bit of code that seems to work just fine. I have tried using ctrl c to copy and ctrl v to paste but in compact framework it does not work...at least not on my device. Dell Axim X3.

The code I am using is working 100% as I wanted it to. Now if someone does know how to effect a copy-paste on this textbox I will have to change my ways as per your suggestions.

PBLack

    Private Sub txtStandArea_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtStandArea.KeyPress
        If Not Char.IsDigit(e.KeyChar) And Not e.KeyChar = "." And Not e.KeyChar = ChrW(8) Then
            Beep()
            e.Handled = True
        Else
            If e.KeyChar = "." And txtStandArea.Text.IndexOf(".") <> -1 Then
                Beep()
                e.Handled = True
            ElseIf e.KeyChar = "." Then
                e.Handled = False
            ElseIf Char.IsDigit(e.KeyChar) Then
                If txtStandArea.Text.IndexOf(".") <> -1 Then
                    If txtStandArea.Text.Length >= txtStandArea.Text.IndexOf(".") + 3 Then
                        Beep()
                        e.Handled = True
                    End If
                End If
            End If
        End If
    End Sub
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 20379325
it is not ctrl-c ctrl-v but if you click and hold the textbox, a menu will appear and let you copy and paste content
0
 

Author Comment

by:PBLack
ID: 20379377
emoreau

that is true if you only program that functionality into it.

PBLack
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 20379474
I thought that this feature was built-in in the textbox control!
0
 

Author Comment

by:PBLack
ID: 20379521
not that I can see
0
 

Author Comment

by:PBLack
ID: 20388030
Hi again

Since I think that I reached a solution that works outside of the expert suggestions I will divide the points up among those who took the time to share their ideas.

Their solutions are great 'food for thought' outside of the compact framework schema.

Regards,

PBLack

ps - experts-exchange should have a compact framework section!
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
vb.net winforms sizing/resolution? 4 40
Iteration Help (Asp.net VB) 5 24
System.net.Sockets Error 5 26
vb.net convert long time to mm:ss 23 10
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
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.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

809 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