G
Gary Simpson
Guest
Hi Good People,
I an having issues with calculations on my form, I am trying to add certain Sum Values textboxes first and then putting the answer in a label. this part works fine.
But when I add values to other Textboxes and I need results of those values, plus the values of the first Label.
When I try this Nothing happens.
But if the value of the first Label = 0.00, and I insert values into the other textboxes the values then the sums begin to work. Please Can somebody check my code to see where I am going wrong.
On my Form1 I have 2 Group boxes
in Groupbox1
(groupbox1) 5 textboxes and 1 label Called
txtPartPrice1
txtPartPrice2
txtPartPrice3
txtPartPrice4
txtPartPrice5
lbTotalPartPrice
(groupbox2) 2 textboxes 3 Labels called
txtPriceOfJob
txtDiscount
lbSubTotal
lbVAT
lbTotal
Buttons X 2
cmdRecalculate
cmdClose
Please can you help Kind Regards Gary
The Code I have is... Plus two Pictures
Option Strict On
Option Explicit On
Public Class Form1
Private Sub cmdClose_Click(sender As Object, e As EventArgs) Handles cmdClose.Click
Application.Exit()
End Sub
Function GetDouble(s As String) As Double
Dim v As Double = 0.0
If Double.TryParse(s, v) Then Return v
Return 0.0
End Function
Function GetInteger(s As String) As Integer
Dim v As Integer = 0
If Integer.TryParse(s, v) Then Return v
Return 0
End Function
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
txtPartPrice1.Enabled = True
txtPartPrice1.Text = "0.00"
txtPartPrice2.Enabled = True
txtPartPrice2.Text = "0.00"
txtPartPrice3.Enabled = True
txtPartPrice3.Text = "0.00"
txtPartPrice4.Enabled = True
txtPartPrice4.Text = "0.00"
txtPartPrice5.Enabled = True
txtPartPrice5.Text = "0.00"
lbTotalPartPrice.Text = "0.00"
txtPriceOfJob.Enabled = True
txtPriceOfJob.Text = "0.00"
txtDiscount.Enabled = True
txtDiscount.Text = "0"
End Sub
Private Sub txtPartPrice1_Leave(sender As Object, e As EventArgs) Handles txtPartPrice1.Leave
Dim PartPrice1 As Decimal
If Decimal.TryParse(txtPartPrice1.Text, PartPrice1) Then
lbTotalPartPrice.Text = CStr(PartPrice1)
lbTotalPartPrice.Text = FormatCurrency(lbTotalPartPrice.Text)
End If
End Sub
Private Sub txtPartPrice2_Leave(sender As Object, e As EventArgs) Handles txtPartPrice2.Leave
Dim PartPrice1 As Decimal = CDec(txtPartPrice1.Text)
Dim PartPrice2 As Decimal
If Decimal.TryParse(txtPartPrice2.Text, PartPrice2) Then
lbTotalPartPrice.Text = CStr(PartPrice1 + PartPrice2)
lbTotalPartPrice.Text = FormatCurrency(lbTotalPartPrice.Text)
End If
End Sub
Private Sub txtPartPrice3_Leave(sender As Object, e As EventArgs) Handles txtPartPrice3.Leave
Dim PartPrice1 As Decimal = CDec(txtPartPrice1.Text)
Dim PartPrice2 As Decimal = CDec(txtPartPrice2.Text)
Dim PartPrice3 As Decimal
If Decimal.TryParse(txtPartPrice3.Text, PartPrice3) Then
lbTotalPartPrice.Text = CStr(PartPrice1 + PartPrice2 + PartPrice3)
lbTotalPartPrice.Text = FormatCurrency(lbTotalPartPrice.Text)
End If
End Sub
Private Sub txtPartPrice4_Leave(sender As Object, e As EventArgs) Handles txtPartPrice4.Leave
Dim PartPrice1 As Decimal = CDec(txtPartPrice1.Text)
Dim PartPrice2 As Decimal = CDec(txtPartPrice2.Text)
Dim PartPrice3 As Decimal = CDec(txtPartPrice3.Text)
Dim PartPrice4 As Decimal
If Decimal.TryParse(txtPartPrice4.Text, PartPrice4) Then
lbTotalPartPrice.Text = CStr(PartPrice1 + PartPrice2 + PartPrice3 + PartPrice4)
lbTotalPartPrice.Text = FormatCurrency(lbTotalPartPrice.Text)
End If
End Sub
Private Sub txtPartPrice5_Leave(sender As Object, e As EventArgs) Handles txtPartPrice5.Leave
Dim PartPrice1 As Decimal = CDec(txtPartPrice1.Text)
Dim PartPrice2 As Decimal = CDec(txtPartPrice2.Text)
Dim PartPrice3 As Decimal = CDec(txtPartPrice3.Text)
Dim PartPrice4 As Decimal = CDec(txtPartPrice4.Text)
Dim PartPrice5 As Decimal
If Decimal.TryParse(txtPartPrice5.Text, PartPrice5) Then
lbTotalPartPrice.Text = CStr(PartPrice1 + PartPrice2 + PartPrice3 + PartPrice4 + PartPrice5)
lbTotalPartPrice.Text = FormatCurrency(lbTotalPartPrice.Text)
End If
End Sub
Private Sub cmdRecalculate_Click(sender As Object, e As EventArgs) Handles cmdRecalculate.Click
Dim PartPrice1 As Decimal = CDec(txtPartPrice1.Text)
Dim PartPrice2 As Decimal = CDec(txtPartPrice2.Text)
Dim PartPrice3 As Decimal = CDec(txtPartPrice3.Text)
Dim PartPrice4 As Decimal = CDec(txtPartPrice4.Text)
Dim PartPrice5 As Decimal
If Decimal.TryParse(txtPartPrice5.Text, PartPrice5) Then
lbTotalPartPrice.Text = CStr(PartPrice1 + PartPrice2 + PartPrice3 + PartPrice4 + PartPrice5)
'lbTotalPartPrice.Text = FormatCurrency(lbTotalPartPrice.Text)
End If
End Sub
Private Sub txtPriceOfJob_TextChanged(sender As Object, e As EventArgs) Handles txtPriceOfJob.TextChanged
If CDbl(lbTotalPartPrice.Text) = GetDouble("£0.00") Then
lbSubTotal.Text = txtPriceOfJob.Text
lbVAT.Text = CStr(CDbl(lbSubTotal.Text) * My.Settings.VATPrice / 100)
Dim Sub1 As Double = CDbl(lbSubTotal.Text)
Dim Sub2 As Double = CDbl(lbVAT.Text)
lbTotal.Text = CStr(Sub1 + Sub2)
ElseIf CDbl(lbTotalPartPrice.Text) >= GetDouble("£0.00") Then
Dim Sub1 As Double = GetDouble(lbSubTotal.Text)
Dim Sub2 As Double = GetDouble(lbVAT.Text)
Dim Sub3 As Double = GetDouble(lbTotalPartPrice.Text)
lbTotal.Text = CStr(GetDouble(lbTotalPartPrice.Text) + Sub1 + Sub2)
'lbTotal.Text = CStr(Sub1 + Sub2 + Sub3)
End If
End Sub
Private Sub txtDiscount_Leave(sender As Object, e As EventArgs) Handles txtDiscount.Leave
'Get Cost of job from txtPriceOfJob
Dim costOfJob As Double = GetDouble(txtPriceOfJob.Text)
'Get Discount from txtDiscount
Dim Mydiscount As Double = GetDouble(txtDiscount.Text)
'Check if is numbers inserted
If IsNumeric(txtPriceOfJob.Text) Then
Dim sub1 As Double = costOfJob
Dim sub2 As Double = sub1 - (sub1 / 100 * Mydiscount)
lbSubTotal.Text = CStr(sub2)
Dim Sum1 As Double = GetDouble(lbSubTotal.Text)
'Get VAT rate from settings
Dim Sum2 As Double = GetDouble(CStr(My.Settings.VATPrice))
lbVAT.Text = CStr(Sum1 * Sum2 / 100)
Dim Sum3 As Double = GetDouble(lbSubTotal.Text)
Dim Sum4 As Double = GetDouble(lbVAT.Text)
lbTotal.Text = CStr(Sum3 + Sum4)
lbSubTotal.Text = FormatCurrency(lbSubTotal.Text)
lbVAT.Text = FormatCurrency(lbVAT.Text)
lbTotal.Text = FormatCurrency(lbTotal.Text)
Else
MessageBox.Show("Please Enter valid Numbers only", "Please use Numbers only", MessageBoxButtons.OK, MessageBoxIcon.Information)
txtPriceOfJob.Clear()
txtPriceOfJob.Focus()
End If
End Sub
Private Sub lbSubTotal_TextChanged(sender As Object, e As EventArgs) Handles lbSubTotal.TextChanged
If CDbl(lbTotalPartPrice.Text) = GetDouble("£0.00") Then
lbSubTotal.Text = txtPriceOfJob.Text
lbVAT.Text = CStr(CDbl(lbSubTotal.Text) * My.Settings.VATPrice / 100)
Dim Sub1 As Double = CDbl(lbSubTotal.Text)
Dim Sub2 As Double = CDbl(lbVAT.Text)
lbTotal.Text = CStr(Sub1 + Sub2)
End If
End Sub
End Class
Gary Simpson
Continue reading...
I an having issues with calculations on my form, I am trying to add certain Sum Values textboxes first and then putting the answer in a label. this part works fine.
But when I add values to other Textboxes and I need results of those values, plus the values of the first Label.
When I try this Nothing happens.
But if the value of the first Label = 0.00, and I insert values into the other textboxes the values then the sums begin to work. Please Can somebody check my code to see where I am going wrong.
On my Form1 I have 2 Group boxes
in Groupbox1
(groupbox1) 5 textboxes and 1 label Called
txtPartPrice1
txtPartPrice2
txtPartPrice3
txtPartPrice4
txtPartPrice5
lbTotalPartPrice
(groupbox2) 2 textboxes 3 Labels called
txtPriceOfJob
txtDiscount
lbSubTotal
lbVAT
lbTotal
Buttons X 2
cmdRecalculate
cmdClose
Please can you help Kind Regards Gary
The Code I have is... Plus two Pictures
Option Strict On
Option Explicit On
Public Class Form1
Private Sub cmdClose_Click(sender As Object, e As EventArgs) Handles cmdClose.Click
Application.Exit()
End Sub
Function GetDouble(s As String) As Double
Dim v As Double = 0.0
If Double.TryParse(s, v) Then Return v
Return 0.0
End Function
Function GetInteger(s As String) As Integer
Dim v As Integer = 0
If Integer.TryParse(s, v) Then Return v
Return 0
End Function
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
txtPartPrice1.Enabled = True
txtPartPrice1.Text = "0.00"
txtPartPrice2.Enabled = True
txtPartPrice2.Text = "0.00"
txtPartPrice3.Enabled = True
txtPartPrice3.Text = "0.00"
txtPartPrice4.Enabled = True
txtPartPrice4.Text = "0.00"
txtPartPrice5.Enabled = True
txtPartPrice5.Text = "0.00"
lbTotalPartPrice.Text = "0.00"
txtPriceOfJob.Enabled = True
txtPriceOfJob.Text = "0.00"
txtDiscount.Enabled = True
txtDiscount.Text = "0"
End Sub
Private Sub txtPartPrice1_Leave(sender As Object, e As EventArgs) Handles txtPartPrice1.Leave
Dim PartPrice1 As Decimal
If Decimal.TryParse(txtPartPrice1.Text, PartPrice1) Then
lbTotalPartPrice.Text = CStr(PartPrice1)
lbTotalPartPrice.Text = FormatCurrency(lbTotalPartPrice.Text)
End If
End Sub
Private Sub txtPartPrice2_Leave(sender As Object, e As EventArgs) Handles txtPartPrice2.Leave
Dim PartPrice1 As Decimal = CDec(txtPartPrice1.Text)
Dim PartPrice2 As Decimal
If Decimal.TryParse(txtPartPrice2.Text, PartPrice2) Then
lbTotalPartPrice.Text = CStr(PartPrice1 + PartPrice2)
lbTotalPartPrice.Text = FormatCurrency(lbTotalPartPrice.Text)
End If
End Sub
Private Sub txtPartPrice3_Leave(sender As Object, e As EventArgs) Handles txtPartPrice3.Leave
Dim PartPrice1 As Decimal = CDec(txtPartPrice1.Text)
Dim PartPrice2 As Decimal = CDec(txtPartPrice2.Text)
Dim PartPrice3 As Decimal
If Decimal.TryParse(txtPartPrice3.Text, PartPrice3) Then
lbTotalPartPrice.Text = CStr(PartPrice1 + PartPrice2 + PartPrice3)
lbTotalPartPrice.Text = FormatCurrency(lbTotalPartPrice.Text)
End If
End Sub
Private Sub txtPartPrice4_Leave(sender As Object, e As EventArgs) Handles txtPartPrice4.Leave
Dim PartPrice1 As Decimal = CDec(txtPartPrice1.Text)
Dim PartPrice2 As Decimal = CDec(txtPartPrice2.Text)
Dim PartPrice3 As Decimal = CDec(txtPartPrice3.Text)
Dim PartPrice4 As Decimal
If Decimal.TryParse(txtPartPrice4.Text, PartPrice4) Then
lbTotalPartPrice.Text = CStr(PartPrice1 + PartPrice2 + PartPrice3 + PartPrice4)
lbTotalPartPrice.Text = FormatCurrency(lbTotalPartPrice.Text)
End If
End Sub
Private Sub txtPartPrice5_Leave(sender As Object, e As EventArgs) Handles txtPartPrice5.Leave
Dim PartPrice1 As Decimal = CDec(txtPartPrice1.Text)
Dim PartPrice2 As Decimal = CDec(txtPartPrice2.Text)
Dim PartPrice3 As Decimal = CDec(txtPartPrice3.Text)
Dim PartPrice4 As Decimal = CDec(txtPartPrice4.Text)
Dim PartPrice5 As Decimal
If Decimal.TryParse(txtPartPrice5.Text, PartPrice5) Then
lbTotalPartPrice.Text = CStr(PartPrice1 + PartPrice2 + PartPrice3 + PartPrice4 + PartPrice5)
lbTotalPartPrice.Text = FormatCurrency(lbTotalPartPrice.Text)
End If
End Sub
Private Sub cmdRecalculate_Click(sender As Object, e As EventArgs) Handles cmdRecalculate.Click
Dim PartPrice1 As Decimal = CDec(txtPartPrice1.Text)
Dim PartPrice2 As Decimal = CDec(txtPartPrice2.Text)
Dim PartPrice3 As Decimal = CDec(txtPartPrice3.Text)
Dim PartPrice4 As Decimal = CDec(txtPartPrice4.Text)
Dim PartPrice5 As Decimal
If Decimal.TryParse(txtPartPrice5.Text, PartPrice5) Then
lbTotalPartPrice.Text = CStr(PartPrice1 + PartPrice2 + PartPrice3 + PartPrice4 + PartPrice5)
'lbTotalPartPrice.Text = FormatCurrency(lbTotalPartPrice.Text)
End If
End Sub
Private Sub txtPriceOfJob_TextChanged(sender As Object, e As EventArgs) Handles txtPriceOfJob.TextChanged
If CDbl(lbTotalPartPrice.Text) = GetDouble("£0.00") Then
lbSubTotal.Text = txtPriceOfJob.Text
lbVAT.Text = CStr(CDbl(lbSubTotal.Text) * My.Settings.VATPrice / 100)
Dim Sub1 As Double = CDbl(lbSubTotal.Text)
Dim Sub2 As Double = CDbl(lbVAT.Text)
lbTotal.Text = CStr(Sub1 + Sub2)
ElseIf CDbl(lbTotalPartPrice.Text) >= GetDouble("£0.00") Then
Dim Sub1 As Double = GetDouble(lbSubTotal.Text)
Dim Sub2 As Double = GetDouble(lbVAT.Text)
Dim Sub3 As Double = GetDouble(lbTotalPartPrice.Text)
lbTotal.Text = CStr(GetDouble(lbTotalPartPrice.Text) + Sub1 + Sub2)
'lbTotal.Text = CStr(Sub1 + Sub2 + Sub3)
End If
End Sub
Private Sub txtDiscount_Leave(sender As Object, e As EventArgs) Handles txtDiscount.Leave
'Get Cost of job from txtPriceOfJob
Dim costOfJob As Double = GetDouble(txtPriceOfJob.Text)
'Get Discount from txtDiscount
Dim Mydiscount As Double = GetDouble(txtDiscount.Text)
'Check if is numbers inserted
If IsNumeric(txtPriceOfJob.Text) Then
Dim sub1 As Double = costOfJob
Dim sub2 As Double = sub1 - (sub1 / 100 * Mydiscount)
lbSubTotal.Text = CStr(sub2)
Dim Sum1 As Double = GetDouble(lbSubTotal.Text)
'Get VAT rate from settings
Dim Sum2 As Double = GetDouble(CStr(My.Settings.VATPrice))
lbVAT.Text = CStr(Sum1 * Sum2 / 100)
Dim Sum3 As Double = GetDouble(lbSubTotal.Text)
Dim Sum4 As Double = GetDouble(lbVAT.Text)
lbTotal.Text = CStr(Sum3 + Sum4)
lbSubTotal.Text = FormatCurrency(lbSubTotal.Text)
lbVAT.Text = FormatCurrency(lbVAT.Text)
lbTotal.Text = FormatCurrency(lbTotal.Text)
Else
MessageBox.Show("Please Enter valid Numbers only", "Please use Numbers only", MessageBoxButtons.OK, MessageBoxIcon.Information)
txtPriceOfJob.Clear()
txtPriceOfJob.Focus()
End If
End Sub
Private Sub lbSubTotal_TextChanged(sender As Object, e As EventArgs) Handles lbSubTotal.TextChanged
If CDbl(lbTotalPartPrice.Text) = GetDouble("£0.00") Then
lbSubTotal.Text = txtPriceOfJob.Text
lbVAT.Text = CStr(CDbl(lbSubTotal.Text) * My.Settings.VATPrice / 100)
Dim Sub1 As Double = CDbl(lbSubTotal.Text)
Dim Sub2 As Double = CDbl(lbVAT.Text)
lbTotal.Text = CStr(Sub1 + Sub2)
End If
End Sub
End Class
Gary Simpson
Continue reading...