|
-
Jun 8th, 2004, 12:37 PM
#1
Thread Starter
Addicted Member
Format("$#####0.00) FIGURED OUT
I am trying to get my grpTotalSales.Text and grpAverageSales.Text to display a dollar sign and then a money value. It works for my gross pay but it doesn't work for the others. Here is the code:
VB Code:
Private Sub btnGross_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnGross.Click
' name abbreviations
SA = Val(nudSales.Text)
SP = Val(grpSalespersons.Text)
TS = Val(grpTotalSales.Text)
' Compute value and display in Gross Pay
GP = BP + SA * CM
txtGross.Text = Format(GP, "$#####0.00")
' Compute value and display in Salespersons
grpSalespersons.Text = (SP + 1).ToString
' Compute value and display in Total Sales
grpTotalSales.Text = (SA + TS).ToString
grpTotalSales.Text = Format(TS, "$#####0.00")
' Compute value and display in Average Sales
grpAverageSales.Text = Format("$#####0.00")
If SP = 0 Then
grpAverageSales.Text = (SA).ToString
Else
grpAverageSales.Text = ((SA + TS) / (SP + 1)).ToString
End If
End Sub
Last edited by twisted; Jun 9th, 2004 at 12:12 PM.
Twisted
-
Jun 8th, 2004, 12:51 PM
#2
PowerPoster
Hi,
Try, for example;
TextGross.Text = FormatCurrency(GP, 2)
By the way, if you are not previously updating SP at any time, your code:
If SP = 0 Then
grpAverageSales.Text = (SA).ToString
Else
grpAverageSales.Text = ((SA + TS) / (SP + 1)).ToString
End If
is unnecessary, as you can simply use
grpAverageSales.Text = ((SA + TS) / (SP + 1)).ToString
Last edited by taxes; Jun 8th, 2004 at 12:55 PM.
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 8th, 2004, 01:04 PM
#3
***?
grpTotalSales.Text = (SA + TS).ToString
grpTotalSales.Text = Format(TS, "$#####0.00")
You do realize line 2 will overwrite tesults from line 1?
TG
-
Jun 8th, 2004, 01:08 PM
#4
Thread Starter
Addicted Member
Originally posted by taxes
Hi,
Try, for example;
TextGross.Text = FormatCurrency(GP, 2)
By the way, if you are not previously updating SP at any time, your code:
If SP = 0 Then
grpAverageSales.Text = (SA).ToString
Else
grpAverageSales.Text = ((SA + TS) / (SP + 1)).ToString
End If
is unnecessary, as you can simply use
grpAverageSales.Text = ((SA + TS) / (SP + 1)).ToString
Nope didn't work!
I also tried this, but then it messes up my math.
VB Code:
grpTotalSales.Text = FormatCurrency((SA + TS), 2)
-
Jun 8th, 2004, 01:10 PM
#5
Thread Starter
Addicted Member
Re: ***?
Originally posted by techgnome
grpTotalSales.Text = (SA + TS).ToString
grpTotalSales.Text = Format(TS, "$#####0.00")
You do realize line 2 will overwrite tesults from line 1?
TG
Actually NO I didn't because I am the true definition of a VB newb, but thanks for clarifying this to me. How would I get it to display the $ with my value. Say Format to look like this $567.65??
-
Jun 8th, 2004, 01:57 PM
#6
Sleep mode
Did you try something like this . This part (ToString("c")) converts the number to the default currency profile on the user system .
VB Code:
Dim int As Integer = 20 + 30
Me.TextBox1.Text = int.ToString("c")
-
Jun 8th, 2004, 02:09 PM
#7
so that would be like so:
grpTotalSales.Text = (SA + TS).ToString("c")
TG
-
Jun 8th, 2004, 02:13 PM
#8
Sleep mode
Originally posted by techgnome
so that would be like so:
grpTotalSales.Text = (SA + TS).ToString("c")
TG
hope it works ....
-
Jun 8th, 2004, 02:13 PM
#9
Thread Starter
Addicted Member
As soon as I add the ("c") to the end it doesn't add SA + TS anymore. It just displays the SA. I had exactly what your code looks like without the ("c") and it works, but as soon as I add the ("c") it doesn't.
-
Jun 8th, 2004, 02:17 PM
#10
Thread Starter
Addicted Member
This might help seeing my code:
VB Code:
Private Sub btnGross_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnGross.Click
' name abbreviations
SA = Val(nudSales.Text)
SP = Val(grpSalespersons.Text)
TS = Val(grpTotalSales.Text)
' Compute value and display in Gross Pay
GP = BP + SA * CM
txtGross.Text = Format(GP, "$#####0.00")
' Compute value and display in Salespersons
grpSalespersons.Text = (SP + 1).ToString
' Compute value and display in Total Sales
grpTotalSales.Text = (SA + TS).ToString
' Compute value and display in Average Sales
If SP = 0 Then
grpAverageSales.Text = (SA).ToString
Else
grpAverageSales.Text = ((SA + TS) / (SP + 1)).ToString
End If
End Sub
-
Jun 8th, 2004, 02:17 PM
#11
Sleep mode
Originally posted by twisted
As soon as I add the ("c") to the end it doesn't add SA + TS anymore. It just displays the SA. I had exactly what your code looks like without the ("c") and it works, but as soon as I add the ("c") it doesn't.
Add the two numbers in an integer or decimal type depends on the number format you'll get . Then use the ToString like shown before .
-
Jun 8th, 2004, 02:26 PM
#12
Thread Starter
Addicted Member
Originally posted by Pirate
Add the two numbers in an integer or decimal type depends on the number format you'll get . Then use the ToString like shown before .
You lost me! I did however add the ("c") behind my AverageSales and it worked fine, just won't work with my TotalSales:
VB Code:
If SP = 0 Then
grpAverageSales.Text = (SA).ToString("c")
Else
grpAverageSales.Text = ((SA + TS) / (SP + 1)).ToString("c")
End If
-
Jun 8th, 2004, 02:49 PM
#13
Sleep mode
We both posted at the same time .
ToString("c") should be the last step and alone before assinging the total to the TextBox . So ,
VB Code:
If SP = 0 Then
grpAverageSales.Text = (SA).ToString("c")
Else
Dim T As Integer =((SA + TS) / (SP + 1))
grpAverageSales.Text = T.ToString("c")
End If
Try this
-
Jun 8th, 2004, 03:15 PM
#14
Thread Starter
Addicted Member
Now do you know why this doesn't work with this:
VB Code:
grpTotalSales.Text = (SA + TS).ToString("c")
It doesn't add for me if I add the "c"
-
Jun 8th, 2004, 03:36 PM
#15
Sleep mode
Originally posted by twisted
Now do you know why this doesn't work with this:
VB Code:
grpTotalSales.Text = (SA + TS).ToString("c")
It doesn't add for me if I add the "c"
It must be something wrong with your logic . It does work fine for me . Try to set breakpoints to see the values of SA and TS .
-
Jun 8th, 2004, 06:18 PM
#16
PowerPoster
Hi twisted,
When you say
TextGross.Text = FormatCurrency(GP, 2)
does not work, what result do you get? I have tried it with various combinations of decimal places and it works fine every time.
From your other threads I assume that you declared GP as a Double.
I suppose the $ is your default currency?
You are using the 2003 version of .NET arn't you?
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, 05:13 AM
#17
PowerPoster
Originally posted by Pirate
It must be something wrong with your logic . It does work fine for me . Try to set breakpoints to see the values of SA and TS .
Works OK for me also.
twisted.
Just a thought. Did you begin this project in VB6 and then convert it to VB.NET?
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, 08:27 AM
#18
Thread Starter
Addicted Member
Originally posted by taxes
Hi twisted,
When you say
TextGross.Text = FormatCurrency(GP, 2)
does not work, what result do you get?
I already got my txtGross.Text to work. I'm trying to get grpTotalSales.Text to do the same, but it won't.
It just doesn't do anything. My total sales still calculates fine, but it doesn't give me a $ sign or the money decimal places. For example: it shows up 800 instead of how I want it $800.00. Here is how my code looks:
VB Code:
Private Sub btnGross_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnGross.Click
' name abbreviations
SA = Val(nudSales.Text)
SP = Val(grpSalespersons.Text)
TS = Val(grpTotalSales.Text)
' Compute value and display in Gross Pay
GP = BP + SA * CM
txtGross.Text = Format(GP, "c")
' Compute value and display in Salespersons
grpSalespersons.Text = (SP + 1).ToString
' Compute value and display in Total Sales
grpTotalSales.Text = FormatCurrency(TS, 2)
grpTotalSales.Text = (SA + TS).ToString
' Compute value and display in Average Sales
If SP = 0 Then
grpAverageSales.Text = (SA).ToString("c")
Else
grpAverageSales.Text = ((SA + TS) / (SP + 1)).ToString("c")
End If
End Sub
-
Jun 9th, 2004, 08:35 AM
#19
PowerPoster
Hi,
OK. What you are doing wrong in this case is
" grpTotalSales.Text = FormatCurrency(TS, 2)
grpTotalSales.Text = (SA + TS).ToString
"
The second line overwrites the first. What you want is the single line
grpTotalSales.Text=FormatCurrency(SA+TS, 2)
or
grpTotalSales.Text=(SA+TS).ToString("c")
or
grpTotalSales.Text = Format(SA+TS, "c")
For the future, I suggest you pick one method and stick to it.
Last edited by taxes; Jun 9th, 2004 at 08:39 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, 08:56 AM
#20
Thread Starter
Addicted Member
Weird! As soon as I try either one of those my SA doesn't add to TS. It just simply display SA. When I leave it like this;
grpTotalSales.Text = (SA + TS).ToString
it works fine. But I can't have no currency and can't figure out why only the Total Sales won't allow me to do this:
grpTotalSales.Text = (SA + TS).ToString("c")
All the others work in my code, just not TotalSales:
VB Code:
Private Sub btnGross_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnGross.Click
' name abbreviations
SA = Val(nudSales.Text)
SP = Val(grpSalespersons.Text)
TS = Val(grpTotalSales.Text)
' Compute value and display in Gross Pay
GP = BP + SA * CM
txtGross.Text = Format(GP, "c")
' Compute value and display in Salespersons
grpSalespersons.Text = (SP + 1).ToString
' Compute value and display in Total Sales
grpTotalSales.Text = (SA + TS).ToString
' Compute value and display in Average Sales
If SP = 0 Then
grpAverageSales.Text = (SA).ToString("c")
Else
grpAverageSales.Text = ((SA + TS) / (SP + 1)).ToString("c")
End If
End Sub
-
Jun 9th, 2004, 09:12 AM
#21
grpTotalSales.Text = FormatCurrency(TS, 2)
grpTotalSales.Text = (SA + TS).ToString
You're duplicating the code again.....
grpTotalSales.Text = FormatCurrency(SA + TS)
One line that's it... what was happening was you were setting it right, then overwriting it....
TG
EDIT: Whoopse, never mind taxes beat me to it.
-
Jun 9th, 2004, 09:37 AM
#22
Thread Starter
Addicted Member
I know you guys are right and my code even looks right, but everytime I format TotalSales to show currency it doesn't add SA + TS anymore. It does display currency correct but only SA. Say if I enter 800.00 in SA then TS will display $800.00. Which is perfect, but when I enter a new value in SA, say 200.00 then I should get TS to be $1,000.00 but I'm getting the value of SA spit back at me $200.00. Before when I just have:
grpTotalSales.Text = (SA + TS).ToString
then it calculates fine, just doesn't have currency. WHICH I NEED!
It has to be something wrong somewhere in my code that is causing this to not work. I know I should be able to just add the "c" and be fine, like I did with the others. Here is my code and I will put in red where my problem is.
VB Code:
Option Strict On
Public Class frmGrossPayCalculator
Inherits System.Windows.Forms.Form
' Assignment 2: Gross Pay Calculator
' Programmer: Brad von Oven
' Date: June 11, 2004
' Purpose: Create an application to display
' a salesperson’s gross pay with base
' and commission based on the following:
' Base pay is $500; commission is 6% of
' sales (use constants to represent these).
' The user enters the salesperson’s total sales;
' keep track of each salesperson’s sales and the
' number of salespersons. Display the current
' person’s gross pay and the summary of:
' total salespersons, total amount of all sales,
' and average sales per salesperson.
' Set up constants for Base pay(BP) = $500 and commission(CM) is 6%
Const BP As Double = 500.0
Const CM As Double = 0.06
' Set up variables for sales, gross pay, salespersons, total sales,
' and average sales.
Dim SA, GP, SP, TS, AV As Double
Private Sub btnExit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnExit.Click
End
End Sub
Private Sub btnGross_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnGross.Click
' name abbreviations
SA = Val(nudSales.Text)
SP = Val(grpSalespersons.Text)
TS = Val(grpTotalSales.Text)
' Compute value and display in Gross Pay
GP = BP + SA * CM
txtGross.Text = Format(GP, "c")
' Compute value and display in Salespersons
grpSalespersons.Text = (SP + 1).ToString
' Compute value and display in Total Sales
[COLOR=red]grpTotalSales.Text = (SA + TS).ToString("c")[/COLOR]
' Compute value and display in Average Sales
If SP = 0 Then
grpAverageSales.Text = (SA).ToString("c")
Else
grpAverageSales.Text = ((SA + TS) / (SP + 1)).ToString("c")
End If
End Sub
Private Sub btnReset_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReset.Click
nudSales.Text = "1.0"
txtGross.Text = ""
nudSales.Focus()
End Sub
Private Sub btnResetAll_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnResetAll.Click
SA = 1.0
txtGross.Text = ""
grpSalespersons.Text = ""
grpTotalSales.Text = ""
grpAverageSales.Text = ""
nudSales.Focus()
End Sub
Private Sub nudSales_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles nudSales.LostFocus
nudSales.Text = Format(nudSales.Value, "#,###.##")
End Sub
End Class
Thanks for all the help guys!
-
Jun 9th, 2004, 10:02 AM
#23
PowerPoster
HI,
OK. I've got it!!!!!
When you set the grpTotalSales.Text to include a currence sign, that means the result of val(grpTotalSales.Text will always be 0, because this expression gives the numerical value of all characters BEFORE the first non numerical character.
I am working on a way round this and will come back to you
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:27 AM
#24
PowerPoster
Hi,
If you want to maintain the currency sign then you will have to us two form scope variables.
The following works:
In the General Section of the form
Dim SP, TS As Double
Then amend your code to
VB Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
' name abbreviations
Dim CM As Double = 0.05
Dim BP As Double = 3000
Dim SA, GP As Double
SP = SP + 1
SA = Val(nudSales.Text)
TS = TS + SA
' Compute value and display in Gross Pay
GP = BP + TS * CM
txtGross.Text = Format(GP, "c")
' Compute value and display in Salespersons
grpSalespersons.Text = (SP).ToString
' Compute value and display in Total Sales
grpTotalSales.Text = (TS).ToString("c")
' Compute value and display in Average Sales
grpAverageSales.Text = (TS / SP).ToString("c")
End Sub
That should get you there, but other people may have other solutions.
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, 11:44 AM
#25
Thread Starter
Addicted Member
Worked awesome! But now I ran into a new problem with this. My Reset All value will reset everything blank which is good, but when I try to type new values in nudSales it retains all the old values and calculates using them. How do I get my Reset all to wipe out all the old values and start from scratch again! Here is what I did with my code:
VB Code:
Dim SA, GP, SP, TS As Double
Private Sub btnExit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnExit.Click
End
End Sub
Private Sub btnGross_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnGross.Click
' name abbreviations
SP = SP + 1
SA = Val(nudSales.Text)
TS = TS + SA
' Compute value and display in Gross Pay
GP = BP + TS * CM
txtGross.Text = Format(GP, "c")
' Compute value and display in Salespersons
grpSalespersons.Text = (SP).ToString
' Compute value and display in Total Sales
grpTotalSales.Text = (TS).ToString("c")
' Compute value and display in Average Sales
grpAverageSales.Text = (TS / SP).ToString("c")
End Sub
Private Sub btnReset_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReset.Click
nudSales.Text = "1.0"
txtGross.Text = ""
nudSales.Focus()
End Sub
Private Sub btnResetAll_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnResetAll.Click
SA = 1.0
txtGross.Text = ""
grpSalespersons.Text = ""
grpTotalSales.Text = ""
grpAverageSales.Text = ""
nudSales.Focus()
End Sub
Private Sub nudSales_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles nudSales.LostFocus
nudSales.Text = Format(nudSales.Value, "#,###.##")
End Sub
End Class
-
Jun 9th, 2004, 11:51 AM
#26
You need to clear out the variable too, not just the text boxes.
TG
-
Jun 9th, 2004, 11:54 AM
#27
Thread Starter
Addicted Member
That's what I thought, so I tried to use:
SP = ""
but Option Strict is On so how would I write that??
-
Jun 9th, 2004, 11:58 AM
#28
Since SP is a number, set it to 0.
SP = 0
TG
-
Jun 9th, 2004, 12:12 PM
#29
Thread Starter
Addicted Member
Originally posted by taxes
Hi,
If you want to maintain the currency sign then you will have to us two form scope variables.
The following works:
In the General Section of the form
Dim SP, TS As Double
Then amend your code to
VB Code:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
' name abbreviations
Dim CM As Double = 0.05
Dim BP As Double = 3000
Dim SA, GP As Double
SP = SP + 1
SA = Val(nudSales.Text)
TS = TS + SA
' Compute value and display in Gross Pay
GP = BP + TS * CM
txtGross.Text = Format(GP, "c")
' Compute value and display in Salespersons
grpSalespersons.Text = (SP).ToString
' Compute value and display in Total Sales
grpTotalSales.Text = (TS).ToString("c")
' Compute value and display in Average Sales
grpAverageSales.Text = (TS / SP).ToString("c")
End Sub
That should get you there, but other people may have other solutions.
You changed my GP formula! I was fliping out, thinking what Now? Anyway, I changed it back to GP = BP + SA * CM
Everything is absolutely perfect! This thread is finally closed. Thanks for all the help guys. More projects to come
-
Jun 9th, 2004, 04:32 PM
#30
PowerPoster
Hi,
Sorry, I've taken a few hours off to play tennis.
"You changed my GP formula! I was fliping out, thinking what Now? Anyway, I changed it back to GP = BP + SA * CM"
Surely if you use SA instead of TS you are calculating your commission on your last sales entry only and not on your total sales.
I wonder if you have done something to upset it in your reset code.
your reset codes should be
VB Code:
Private Sub btnReset_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReset.Click
nudSales.Text = ""
txtGross.Text = ""
nudSales.Focus()
End Sub
Private Sub btnResetAll_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnResetAll.Click
SP = 0
TS = 0
grpSalespersons.Text = ""
grpTotalSales.Text = ""
grpAverageSales.Text = ""
btnReset.PerformClick()
End Sub
You do not need to reset SA as that is done automatically in the Button_Click event.
Now you have got this far, note that the only box in which entries are made by the operator is the nudSales textbox. Therefore, All THE OTHER textboxes SHOULD be Labels. Otherwise the operator might make erroneous entries and defeat the program
Last edited by taxes; Jun 9th, 2004 at 05:03 PM.
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.
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
|