Solved

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

Posted on 2010-08-15
9
416 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
 
LVL 8

Expert Comment

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

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 17

Assisted Solution

by:Zhaolai
Zhaolai earned 100 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 47

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 400 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

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

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

863 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

23 Experts available now in Live!

Get 1:1 Help Now