Solved

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

Posted on 2010-08-15
9
440 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VB6 calling COM DLL written in Visual Studio 2003 6 60
Read oracle BLOB in vb.net 2 24
Intermittent OleDbConnection Error 20 54
EMAIL RANGES BASED ON CURRENT TIME 12 41
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…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
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…
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

710 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