Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

Vb.net problem help with adding text boxes?

My program will not add txtPart and txtLabor, it says they are strings and cannot be converted to decimal even tho i used CDec

" 'get amount for parts and labor

decLabor = CDec(txtLabor.Text)

decPart = CDec(txtParts.Text)

decPartLabor = decPart + decLabor"

Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click

'declare variables

Dim decOil As Decimal

Dim decFlush As Decimal

Dim decMisc As Decimal

Dim decPart As Decimal

Dim decLabor As Decimal

Dim decPartLabor As Decimal

Dim decTotal As Decimal

Dim decSandL As Decimal

Dim decTax As Decimal

'gets amount for oil change

If chkOil.Checked = True Then

decOil += 26.0

End If

If chkLube.Checked = True Then

decOil += 18.0

End If

'gets amount for flushes

If chkTransmission.Checked = True Then

decFlush += 80.0

End If

If chkRadiator.Checked = True Then

decFlush += 30.0

End If

'gets AMOUNT for misc services

If chkInspection.Checked = True Then

decMisc += 15.0

End If

If chkMuffler.Checked = True Then

decMisc = 100.0

End If

If chkTire.Checked = True Then

decMisc += 20.0

End If

'get amount for parts and labor

decLabor = CDec(txtLabor.Text)

decPart = CDec(txtParts.Text)

decPartLabor = decPart + decLabor

'Gets amount for service and labor

decSandL = decOil + decFlush + decMisc + decFlush + decLabor

'get amount for total

decTotal = decOil + decFlush + decMisc + decFlush + decPartLabor

'get amount for tax

decTax = decPart * 0.06

'display parts

lblParts.Text = decPart.ToString("C")

'display tax

lblTax.Text = decTax.ToString("C")

'display service and labor

lblSAndL.Text = decSandL.ToString("C")

'display total

lblTotal.Text = decTotal.ToString("C")

End Sub

1 Answer

Relevance
  • 9 years ago
    Favorite Answer

    Try this instead:

    Decimal.TryParse(txtLabor.Text, decLabor)

    Make sure that your textbox does not have anything in it that would cause the conversion to fail, like non numeric data. You can test that easily using IsNumeric

    If IsNumeric(txtLabor.Text) = False Then

    Msgbox("Labor must be numeric")

    End If

    Source(s): VB.NET Programmer
Still have questions? Get your answers by asking now.