Results 1 to 11 of 11

Thread: Decimal Places FIGURED OUT

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    May 2004
    Posts
    131

    Decimal Places FIGURED OUT

    I have a Number UpDown box that everytime I type in..say 800 when I leave that box and click another it won't display 800.00

    It just stays 800 even though I have decimal place properties set on 2.

    I want users to type in 800 and when they hit on another box for it to display 800.00

    I tried this but it didn't work:

    Private Sub nudAmount_LostFocus()
    nudAmount.Text = Format(nudAmount.Text, ".00")
    End Sub
    Last edited by twisted; Jun 9th, 2004 at 10:39 AM.
    Twisted

  2. #2
    PowerPoster 2.0 Negative0's Avatar
    Join Date
    Jun 2000
    Location
    Southeastern MI
    Posts
    4,367
    Try this:

    VB Code:
    1. Private Sub NumericUpDown1_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles NumericUpDown1.LostFocus
    2.         NumericUpDown1.Text = Format(NumericUpDown1.Value, "#,###.##")
    3.     End Sub

  3. #3
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    What's the maximum value that can be set?
    and it's wise to handle this validation rule in the validating event not the lost focus .

    here's one way ,
    VB Code:
    1. Private Sub NumericUpDown1_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles NumericUpDown1.Validating
    2.         NumericUpDown1.Value = Decimal.Parse(Me.NumericUpDown1.Text, Globalization.NumberStyles.AllowThousands)
    3.     End Sub

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    May 2004
    Posts
    131
    SEE HOW IT KICKS BACK TO THE DECIMAL PLACES WHEN THEY HIT CALCULATE
    That is the only thing I can't figure out!
    Twisted

  5. #5
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    Originally posted by twisted
    SEE HOW IT KICKS BACK TO THE DECIMAL PLACES WHEN THEY HIT CALCULATE
    That is the only thing I can't figure out!
    Check if the event handler (nudAmount_LostFocus) is attched to nudAmount control ??

    Second , did you try both ways above (they work fine for me) ??

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    May 2004
    Posts
    131
    Originally posted by Pirate
    What's the maximum value that can be set?
    and it's wise to handle this validation rule in the validating event not the lost focus .

    here's one way ,
    VB Code:
    1. Private Sub NumericUpDown1_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles NumericUpDown1.Validating
    2.         NumericUpDown1.Value = Decimal.Parse(Me.NumericUpDown1.Text, Globalization.NumberStyles.AllowThousands)
    3.     End Sub
    It works for nudAmount, but not nudRate and nudYears. I get this error:
    An unhandled exception of type 'System.FormatException' occurred in system.windows.forms.dll

    Additional information: Input string was not in a correct format.

    VB Code:
    1. Option Strict On
    2. Public Class frmFutureCalculator
    3.     Inherits System.Windows.Forms.Form
    4.     ' Assignment 2:     Gross Pay Calculator
    5.     ' Programmer:       Brad von Oven
    6.     ' Date:             June 11, 2004
    7.     ' Purpose:          Create an application to calculate
    8.     '                   the future value of an investment
    9.     '                   given the investment amount, interest rate,
    10.     '                   and number of years invested.  
    11.     '                   Display future value in a read only text box.
    12.     Dim IA, IR, YR, FVC As Double
    13.  
    14.     Private Sub cmdCalculate_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdCalculate.Click
    15.         IA = Val(nudAmount.Text)
    16.         IR = Val(nudRate.Text)
    17.         YR = Val(nudYears.Text)
    18.         'Compute final value and put in text box
    19.         FVC = IA * (1 + IR) ^ YR
    20.         txtFuture.Text = Format(FVC, "$#####0.00")
    21.  
    22.  
    23.     End Sub
    24.  
    25.     Private Sub cmdExit_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdExit.Click
    26.         End
    27.     End Sub
    28.  
    29.     Private Sub cmdReset_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdReset.Click
    30.         nudAmount.Text = "100.00"
    31.         nudRate.Text = "0.015"
    32.         nudYears.Text = "0.5"
    33.         txtFuture.Text = ""
    34.         nudAmount.Focus()
    35.  
    36.     End Sub
    37.  
    38.     Private Sub nudAmount_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles nudAmount.Validating
    39.         nudAmount.Value = Decimal.Parse(Me.nudAmount.Text, Globalization.NumberStyles.AllowThousands)
    40.     End Sub
    41.  
    42.     Private Sub nudRate_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles nudRate.Validating
    43.         nudRate.Value = Decimal.Parse(Me.nudRate.Text, Globalization.NumberStyles.AllowThousands)
    44.     End Sub
    45.  
    46.     Private Sub nudYears_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles nudYears.Validating
    47.         nudYears.Value = Decimal.Parse(Me.nudYears.Text, Globalization.NumberStyles.AllowThousands)
    48.     End Sub
    49. End Class
    Twisted

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    May 2004
    Posts
    131
    This might help. Values are as follows:

    nudAmount Max: 10000 Min: 100

    nudRate Max: .25 Min: .015

    nudYears Max: 15 Min: .5

    I think it might have something to do with the last portion of the validating statement:

    Me.nudRate.Text, Globalization.NumberStyles.AllowThousands
    Twisted

  8. #8
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    Hi,

    In view of all the problems you are having with formatting numbers, I think you should concentrate on using the structure

    (ss & st are declared as Double)

    TextBox1.Text = FormatNumber(ss + st, 2) 'for numbers to 2 decimal places


    and

    TextBox1.Text = FormatCurrency(ss + st, 2) 'for currency signs.


    When you have completed your project you can then investigate all the other ways you have been shown.
    Remember KISS
    Last edited by taxes; Jun 9th, 2004 at 08:54 AM.
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    May 2004
    Posts
    131
    Originally posted by Pirate
    What's the maximum value that can be set?
    and it's wise to handle this validation rule in the validating event not the lost focus .

    here's one way ,
    VB Code:
    1. Private Sub NumericUpDown1_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles NumericUpDown1.Validating
    2.         NumericUpDown1.Value = Decimal.Parse(Me.NumericUpDown1.Text, Globalization.NumberStyles.AllowThousands)
    3.     End Sub
    I just had to change AllowThousands to AllowDecimalPoint
    Twisted

  10. #10
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    Hi,

    Just a thought, If you are going to produce a programme to calculate future valuse of an invested amount, the quickest and shortest way is to use financial formula. You will find them on the net under "Annuity Formula".

    There are formula for interest due monthly, quarterly or annually and in advance or in arrears.
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  11. #11

    Thread Starter
    Addicted Member
    Join Date
    May 2004
    Posts
    131
    I will do in future projects, but since this one is done it's staying just like this. Don't want to make my work any harder then I have to. I will use in the future though. Thanks it does look much easier.
    Twisted

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width