Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Textbox -- what is the best way to limit the text box to one decimal in currency format?

Posted on 2010-08-15
9
Medium Priority
?
454 Views
Last Modified: 2013-11-26
I would like to limit my end user to one decimal in currency format.

When the user submits the data, it should be in the format of 0.00 where the amount can be any number up to decimal(18, 2)
0
Comment
Question by:jdraggi
9 Comments
 

Expert Comment

by:abrajoe
ID: 33442600
you can use Math.Round() before you submit the value to the database
0
 
LVL 17

Expert Comment

by:Zhaolai
ID: 33442711
Try this:


        If e.KeyCode = Keys.Back OrElse e.KeyCode = Keys.Delete Then    'allow Backspace and Delete Keys
            Exit Sub
        End If
        If e.KeyCode = "-" Then 'allow negative sign as the first character
            If TextBox1.Text.Trim <> "" Then
                e.SuppressKeyPress = True
            End If
        End If
        If TextBox1.Text.Contains(".") Then
            If e.KeyCode = Keys.Decimal Then
                e.SuppressKeyPress = True
                Exit Sub
            End If
            If TextBox1.Text Like "*.##" Then
                e.SuppressKeyPress = True
            End If
        End If

Open in new window

0
 
LVL 17

Expert Comment

by:Zhaolai
ID: 33442715
Oh, the code should be in the KeyDown event:

    Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
        If e.KeyCode = Keys.Back OrElse e.KeyCode = Keys.Delete Then    'allow Backspace and Delete Keys
            Exit Sub
        End If
        If e.KeyCode = "-" Then 'allow negative sign as the first character
            If TextBox1.Text.Trim <> "" Then
                e.SuppressKeyPress = True
            End If
        End If
        If TextBox1.Text.Contains(".") Then
            If e.KeyCode = Keys.Decimal Then
                e.SuppressKeyPress = True
                Exit Sub
            End If
            If TextBox1.Text Like "*.##" Then
                e.SuppressKeyPress = True
            End If
        End If
    End Sub
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 8

Expert Comment

by:PagodNaUtak
ID: 33442933
When submitting the value try this code:
textbox1.text.tostring("0.00")
0
 
LVL 17

Assisted Solution

by:Zhaolai
Zhaolai earned 400 total points
ID: 33443054
Ooops, there is a syntax error in my previous posts.
Sorry for the mistakes.

1. Here is the correct code:

    Private Sub TextBox1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TextBox1.KeyDown
        If e.KeyCode = Keys.Back OrElse e.KeyCode = Keys.Delete Then    'allow Backspace and Delete Keys
            Exit Sub
        End If
        If e.KeyCode = Keys.Space Then 'do not allow spaces
            e.SuppressKeyPress = True
            Exit Sub
        End If
        If e.KeyCode = Keys.Subtract OrElse e.KeyCode = Keys.OemMinus Then 'allow negative sign as the first character
            If TextBox1.Text <> "" AndAlso TextBox1.Text.StartsWith("-") Then
                e.SuppressKeyPress = True
            ElseIf TextBox1.SelectionStart > 0 Then
                e.SuppressKeyPress = True
            End If
            Exit Sub
        End If
        If TextBox1.Text.Contains(".") Then
            If e.KeyCode = Keys.Decimal Then
                e.SuppressKeyPress = True
                Exit Sub
            End If
            If TextBox1.Text Like "*.##" Then
                e.SuppressKeyPress = True
            End If
        End If
    End Sub


2. To submit the value, do this:

Format(Val(TextBox1.Text), "0.00")
0
 
LVL 48

Expert Comment

by:Wayne Taylor (webtubbs)
ID: 33443089
Use the MaskedTextbox control, with a mask of "#.0"...

http://msdn.microsoft.com/en-us/library/kkx4h3az.aspx

Wayne
0
 

Expert Comment

by:haroonali361
ID: 33443601
abcvalue = FormatNumber$(Val(abc.text, 2)
0
 
LVL 18

Expert Comment

by:John (Yiannis) Toutountzoglou
ID: 33444272
Try also this ...working on me ....
Public Function Currency(ByVal sender As System.Object, ByVal eChar As Char) As Boolean
        Dim CheckString As String = "$0123456789."
        If CheckString.IndexOf(eChar) > -1 OrElse eChar = vbBack Then
            If CType(sender, TextBox).Text.IndexOf(".") > -1 Then
                If CType(sender, TextBox).TextLength - CType(sender, TextBox).Text.IndexOf(".") > 2 And eChar <> vbBack Then
                    Return True
                End If
            End If
            If eChar = "$" Then
                If CType(sender, TextBox).TextLength > 0 Then
                    Return True
                Else
                    Return False
                End If
            End If
            If eChar = "." Then
                If CType(sender, TextBox).Text.IndexOf(eChar) > -1 Then
                    Return True
                Else
                    Return False
                End If
            End If
            Return False
        Else
            Return True
        End If
    End Function

Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
        e.Handled = Currency(sender, e.KeyChar)
    End Sub

Open in new window

0
 
LVL 18

Accepted Solution

by:
John (Yiannis) Toutountzoglou earned 1600 total points
ID: 33444275

       Dim CheckString As String = "$0123456789."
        If CheckString.IndexOf(eChar) > -1 OrElse eChar = vbBack Then
            If CType(sender, TextBox).Text.IndexOf(".") > -1 Then
                If CType(sender, TextBox).TextLength - CType(sender, TextBox).Text.IndexOf(".") > 1 And eChar <> vbBack Then   '------>For One Digit After decimal'
                    Return True
                End If
            End If
            If eChar = "$" Then
                If CType(sender, TextBox).TextLength > 0 Then
                    Return True
                Else
                    Return False
                End If
            End If
            If eChar = "." Then
                If CType(sender, TextBox).Text.IndexOf(eChar) > -1 Then
                    Return True
                Else
                    Return False
                End If
            End If
            Return False
        Else
            Return True
        End If
    End Function

Private Sub TextBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
        e.Handled = Currency(sender, e.KeyChar)
    End Sub

Open in new window

0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

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

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…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

783 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