Help With Basic Math...........

Joined
Aug 9, 2005
Messages
9
Location
Georgia
Programming Experience
1-3
I am writing a Property management Program and I have run into a snag and any help with coding would be appriciated. What I want is in figure (A) Add the The "Total Amount of Rent" in Textbox 1, and the "Security Deposit Amount" and have the end result appear in Textbox 3 ("Total Amount Due). If the amount paid in TextBox 4 by the applicant is either more or is than the amount in TextBox 3 I would like to add a Label saying "Refund Due" or "$$ Amount Due". In Figure (B) Add or Subtract the "Total Amount Due" in Textbox 1, and the "Amount Due" in TextBox 2 and the end result appear in TextBox 3 and if the amount in TextBox 3 is either more or is than the amount in TextBox 1 I would like to add a Label saying "Refund Due" or $$ Amount Due. How do I code it? with an If...Then Statement or what?
 

Attachments

  • VB.Net-Calculation-Question.gif
    VB.Net-Calculation-Question.gif
    3.9 KB · Views: 102
Ok, lets start with Figure A:

in BtnCalculate Click Event:

VB.NET:
' Establish Variables for the calculation
Dim decRentAmount as Decimal
Dim decSecurityDeposit as Decimal
Dim decAmountDue as Decimal
Dim decTotalPaid as Decimal
Dim decAmountDueOrRefund as Decimal

' Now assign values to the variables from the TextBoxes
' I will use your naming conventions, but you should give 
' meaningful names to everything even controls.

' I am not including an exception handling here, but it really needs to be done.....
decRentAmount = TextBox1.Text 
decSecurityDeposit = TextBox2.Text
decTotalPaid = TextBox3.Text

' Time for some calculations
decAmountDue = decRentAmount + decSecurityDeposit
decAmountDueOrRefund = decAmountDue - decTotalPaid

' If decAmountDueOrRefund is 0 then we are even, 
' if negative we owe them, if positive they owe us more.

If decAmountDueOrRefund = 0 Then
   Label1.Text = "No Balance Due"
ElseIf dec AmountDueOrRefund < 0 Then
   Label1.Text = Math.Abs(decAmountDueOrRefund) & " Refund Due"
ElseIf decAmoutnDueOrRefund > 0 Then
   Label1.Text = decAmountDueOrRefund & " Due"
Else
   ' Not sure what else could really happen here with proper exception handling, 
   ' but still good practice to include this.
End If

This should be a good start for you and should also provide most of the information needed to do the Figure B portion of your problem. Give it a shot and post any problems yo uhabe with it here.
 
Seems I forgot that part, sorry.

Try this:

VB.NET:
 ' Time for some calculations
decAmountDue = decRentAmount + decSecurityDeposit
TextBox3.Text = decAmonutDue ' Is this the right text box?  why controls should be named
decAmountDueOrRefund = decAmountDue - decTotalPaid
 
I would suggest that you make the output TextBoxes ReadOnly and use NumericUpDown controls for the input. You can set the number of decimal places for the NUD and it will take care of validation for you. The NUD has no Text property but has a Value property that is of type Decimal, so there is no need for validating and converting strings to numbers.
 
I guess what I want to happen (sorry for not explaining before) When the end user clicks the Calculate button the amount due (weather it be 0 or 300) automatically appears along with a label below the last box telling the end user the amount still owed or the amount of the refund. The code I was given helps but the program is still not doing what I want it to do. Any help with code would be greately appriciated.


VB.NET:
[LEFT]' Establish Variables for the calculation
Dim decRentAmount as Decimal
Dim decSecurityDeposit as Decimal
Dim decAmountDue as Decimal
Dim decTotalPaid as Decimal
Dim decAmountDueOrRefund as Decimal[/LEFT]
 
[LEFT]' Now assign values to the variables from the TextBoxes
' I will use your naming conventions, but you should give 
' meaningful names to everything even controls.[/LEFT]
 
[LEFT]' I am not including an exception handling here, but it really needs to be done.....
decRentAmount = TextBox1.Text 
decSecurityDeposit = TextBox2.Text
decTotalPaid = TextBox3.Text[/LEFT]
 
[LEFT]' Time for some calculations
decAmountDue = decRentAmount + decSecurityDeposit
decAmountDueOrRefund = decAmountDue - decTotalPaid[/LEFT]
 
[LEFT]' If decAmountDueOrRefund is 0 then we are even, 
' if negative we owe them, if positive they owe us more.[/LEFT]
 
[LEFT]If decAmountDueOrRefund = 0 Then
Label1.Text = "No Balance Due"
ElseIf dec AmountDueOrRefund < 0 Then
Label1.Text = Math.Abs(decAmountDueOrRefund) & " Refund Due"
ElseIf decAmoutnDueOrRefund > 0 Then
Label1.Text = decAmountDueOrRefund & " Due"
Else
' Not sure what else could really happen here with proper exception handling, 
' but still good practice to include this.
End If[/LEFT]
 
I don't understand why you have two different figures. Are the "Amount Due" and "Amount Paid" TextBoxes in the two figures the same or are you transferring the contents from figure A to figure B, and if so why? In my opinion you shlould just have five fields labeled Rent, Security Deposit, Subtotal, Paid and Due. You would then add the amounts in the the first two fields and place the result in the third. You would then subtract the amount in the fourth field from the amount in the third field to get the final amount due. If that value is less than zero, you would change the last label to read "Refund" and turn it red, and put the absolute value of the total in the last field. If the final value is greater than or equal to zero you would make the last label read "Due" and turn it black, and put the total in the last field. Here's a couple of screenshots and some code of how I see it working.
VB.NET:
	Private Sub calculateButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles calculateButton.Click
		'Add the rent and the deposit to get the subtotal.
		Dim subtotal As Decimal = Me.rentSpinner.Value + Me.depositSpinner.Value

		'Display the subtotal with two decimal places.
		Me.subtotalText.Text = subtotal.ToString("n2")

		'Subtract the amount paid from the subtotal to get the amount due.
		Dim amountDue As Decimal = subtotal - Me.paidSpinner.Value

		'Set the due label appropriately.
		If amountDue < 0 Then
			Me.dueLabel.Text = "R&efund:"
			Me.dueLabel.ForeColor = Color.Red
		Else
			Me.dueLabel.Text = "Du&e:"
			Me.dueLabel.ForeColor = SystemColors.ControlText
		End If

		'Set the amount due or to be refunded.
		Me.dueText.Text = Math.Abs(amountDue).ToString("n2")
	End Sub

	Private Sub clearButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles clearButton.Click
		'Clear all fields.
		Me.rentSpinner.Value = 0
		Me.depositSpinner.Value = 0
		Me.subtotalText.Text = 0.ToString("n2")
		Me.paidSpinner.Value = 0
		Me.dueText.Text = 0.ToString("n2")
		Me.dueLabel.Text = "Du&e:"
		Me.dueLabel.ForeColor = SystemColors.ControlText
	End Sub
I have set the ReadOnly property to True for the two TextBoxes. I have also set the DecimalPlaces property to 2 and the Maximum property to 1000000 for each NumericUpDown. You may obviously want to change certain things but this is a good basis.
 

Attachments

  • Calc1.JPG
    Calc1.JPG
    10.9 KB · Views: 99
  • Calc2.JPG
    Calc2.JPG
    10.9 KB · Views: 98
  • Calc3.JPG
    Calc3.JPG
    11 KB · Views: 96
Back
Top