简体   繁体   English

Visual Studio按钮代码错误

[英]Visual Studio Button code error

This is my first time using visual studio and I'm running into an error with my tax calulator application. 这是我第一次使用Visual Studio,并且我的税金计算器应用程序遇到了错误。 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. 问题是Visual Studio表示由于变量是文本框而无法使用“ *”和“ +”进行乘法行。 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. 如果您需要我为您提供应用程序的完整布局,请询问。

I can see multiple problems in your code. 我可以在您的代码中看到多个问题。 I will explain how the "vs" works.The textboxes that you've made are controls. 我将解释“ vs”的工作原理。您创建的文本框是控件。
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. 您不能只是简单地将它们的值取值。它们具有许多属性,其中之一就是.text,您可以在其中取值。
Another mistake you've made is what you've tried to do with the textboxes.What you type in those textboxes is ..well text. 您犯的另一个错误是您试图对文本框进行操作。在这些文本框中键入的是..well文本。 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. 您必须使用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. cint的作用是将每种类型的数据转换为整数。 Also the .text property lets you to set the value of the control (in our case the label caption) 此外,.text属性还允许您设置控件的值(在本例中为标签标题)

I tried Electric-web's code and ran into a problem with the output. 我尝试了Electric-web的代码,并在输出中遇到了问题。 I changed "CInt" to "CDbl" and the tax calculator worked. 我将“ CInt”更改为“ CDbl”,并且税收计算器正常工作。

    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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM