|
-
Jun 2nd, 2004, 12:27 PM
#1
Thread Starter
Addicted Member
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
-
Jun 2nd, 2004, 01:38 PM
#2
Try this:
VB Code:
Private Sub NumericUpDown1_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles NumericUpDown1.LostFocus
NumericUpDown1.Text = Format(NumericUpDown1.Value, "#,###.##")
End Sub
-
Jun 2nd, 2004, 01:42 PM
#3
Sleep mode
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:
Private Sub NumericUpDown1_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles NumericUpDown1.Validating
NumericUpDown1.Value = Decimal.Parse(Me.NumericUpDown1.Text, Globalization.NumberStyles.AllowThousands)
End Sub
-
Jun 8th, 2004, 03:18 PM
#4
Thread Starter
Addicted Member
-
Jun 8th, 2004, 03:45 PM
#5
Sleep mode
Check if the event handler (nudAmount_LostFocus) is attched to nudAmount control ??
Second , did you try both ways above (they work fine for me) ??
-
Jun 9th, 2004, 08:41 AM
#6
Thread Starter
Addicted Member
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:
Private Sub NumericUpDown1_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles NumericUpDown1.Validating
NumericUpDown1.Value = Decimal.Parse(Me.NumericUpDown1.Text, Globalization.NumberStyles.AllowThousands)
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:
Option Strict On
Public Class frmFutureCalculator
Inherits System.Windows.Forms.Form
' Assignment 2: Gross Pay Calculator
' Programmer: Brad von Oven
' Date: June 11, 2004
' Purpose: Create an application to calculate
' the future value of an investment
' given the investment amount, interest rate,
' and number of years invested.
' Display future value in a read only text box.
Dim IA, IR, YR, FVC As Double
Private Sub cmdCalculate_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdCalculate.Click
IA = Val(nudAmount.Text)
IR = Val(nudRate.Text)
YR = Val(nudYears.Text)
'Compute final value and put in text box
FVC = IA * (1 + IR) ^ YR
txtFuture.Text = Format(FVC, "$#####0.00")
End Sub
Private Sub cmdExit_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdExit.Click
End
End Sub
Private Sub cmdReset_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdReset.Click
nudAmount.Text = "100.00"
nudRate.Text = "0.015"
nudYears.Text = "0.5"
txtFuture.Text = ""
nudAmount.Focus()
End Sub
Private Sub nudAmount_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles nudAmount.Validating
nudAmount.Value = Decimal.Parse(Me.nudAmount.Text, Globalization.NumberStyles.AllowThousands)
End Sub
Private Sub nudRate_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles nudRate.Validating
nudRate.Value = Decimal.Parse(Me.nudRate.Text, Globalization.NumberStyles.AllowThousands)
End Sub
Private Sub nudYears_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles nudYears.Validating
nudYears.Value = Decimal.Parse(Me.nudYears.Text, Globalization.NumberStyles.AllowThousands)
End Sub
End Class
-
Jun 9th, 2004, 08:45 AM
#7
Thread Starter
Addicted Member
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
-
Jun 9th, 2004, 08:50 AM
#8
PowerPoster
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.
-
Jun 9th, 2004, 10:01 AM
#9
Thread Starter
Addicted Member
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:
Private Sub NumericUpDown1_Validating(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles NumericUpDown1.Validating
NumericUpDown1.Value = Decimal.Parse(Me.NumericUpDown1.Text, Globalization.NumberStyles.AllowThousands)
End Sub
I just had to change AllowThousands to AllowDecimalPoint
-
Jun 9th, 2004, 10:34 AM
#10
PowerPoster
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.
-
Jun 9th, 2004, 10:38 AM
#11
Thread Starter
Addicted Member
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|