Solved

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

Posted on 2007-11-29
9
1,175 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
Comment Utility
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 69

Assisted Solution

by:Éric Moreau
Éric Moreau earned 150 total points
Comment Utility
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
Comment Utility
also consider to use the MaskedTextBox control.
0
 

Author Comment

by:PBLack
Comment Utility
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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 69

Expert Comment

by:Éric Moreau
Comment Utility
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
Comment Utility
emoreau

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

PBLack
0
 
LVL 69

Expert Comment

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

Author Comment

by:PBLack
Comment Utility
not that I can see
0
 

Author Comment

by:PBLack
Comment Utility
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

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…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This video discusses moving either the default database or any database to a new volume.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

744 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

17 Experts available now in Live!

Get 1:1 Help Now