0

This is my first time using visual studio and I'm running into an error with my tax calulator application. The problem is that visual studio is saying that the multiplication line using "*" and "+" can not be done due to the variables being text boxes. So my question to the community, is should I change the text box to something else? Or where in my code did I mess up.

Public Class Form1
    Private Sub btnCalc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalc.Click
        If (IsNumeric(txtSale) And IsNumeric(txtSalesTaxRate)) Then
            lblTax = txtSale * txtSalesTaxRate
            lblTotal = txtSale + lblTax
        Else
            MsgBox("Please enter valid numbers, thank you!")
        End If

    End Sub
End Class

If you need me to give you the full layout of my application, do ask.

4
  • 1
    You are reference the textbox not the Text property. Use txtSale.Text instead Commented Jan 5, 2016 at 21:03
  • 2
    Also , you should be working with 'option strict on' :) Commented Jan 5, 2016 at 21:08
  • Thanks Mark Hall, ImDeveloping! I'll give that a try. Commented Jan 5, 2016 at 21:27
  • Remember the .Text property for your labels as well, so that your tax and total will be displayed Commented Jan 5, 2016 at 21:31

2 Answers 2

1

I can see multiple problems in your code. I will explain how the "vs" works.The textboxes that you've made are controls.
You can't just take their value so simple.They have many property and one of them is .text which allows you to take the value inside them.
Another mistake you've made is what you've tried to do with the textboxes.What you type in those textboxes is ..well text. The program can't tell if the value inside is a number or just text. You must convert that value in a number using Cint.
So you're code will look like this:

Public Class Form1
Private Sub btnCalc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalc.Click
    If (IsNumeric(txtSale.text) And IsNumeric(txtSalesTaxRate.text)) Then
        lblTax.text= cint(txtSale.text) * cint(txtSalesTaxRate.text)
        lblTotal.text= cint(txtSale.text) + cint(lblTax.text)
    Else
        MsgBox("Please enter valid numbers, thank you!")
    End If

End Sub
End Class

What cint does is to convert every type of data to integer. Also the .text property lets you to set the value of the control (in our case the label caption)

Sign up to request clarification or add additional context in comments.

2 Comments

.Text is not a function, it's a property. Just a little perfectionist's note. ;)
.Text is not a function, it is a Property. And converting a Tax Rate to integer may be incorrect if it has decimals. Other text may fail if the user enters other chars, so TryParse should be used
1

I tried Electric-web's code and ran into a problem with the output. I changed "CInt" to "CDbl" and the tax calculator worked.

    Public Class Form1
Private Sub btnCalc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalc.Click
    If (IsNumeric(txtSale.text) And IsNumeric(txtSalesTaxRate.text)) Then
        lblTax.text= cdbl(txtSale.text) * cdbl(txtSalesTaxRate.text)
        lblTotal.text= cdbl(txtSale.text) + cdbl(lblTax.text)
    Else
        MsgBox("Please enter valid numbers, thank you!")
    End If

End Sub
End Class

1 Comment

You can also try convert.ToInt or to double is you want the numbers not to be rounded

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.