Solved

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

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

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
 

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 69

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 69

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
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…

943 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

6 Experts available now in Live!

Get 1:1 Help Now