Visual Basic窗体中的计算和转换问题

时间:2010-10-06 19:29:07

标签: visual-studio-2008 type-conversion

在下面的代码中,我得到“从字符串转换”到“输入'Double'无效。”错误。任何人都可以让我知道为什么会这样吗?感谢。

Public Class Form1 Dim SalesDecimal As Decimal Const BasePay As Decimal = 250D Const CommissionRate As Decimal = 0.15D Dim Quota As Integer = 1000 Dim TotalSalesAmt, TotalComAmt, GrandTotal As Decimal

Private Sub PayToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PayToolStripMenuItem.Click
    SalesDecimal = Decimal.Parse(TextBox3.Text)
    TextBox2.Text = Decimal.Parse(CommissionCalc(SalesDecimal))
    ***TextBox4.Text = Decimal.Parse(CommissionRate * SalesDecimal)***
    If TextBox3.Text < 1000 Then
        TextBox4.Text = ""
    End If
    TotalSalesAmt += TextBox3.Text
    TotalComAmt += TextBox4.Text
    GrandTotal += TextBox2.Text
End Sub
Private Function CommissionCalc(ByVal Sales As Integer) As Decimal
    Dim Total = (SalesDecimal * CommissionRate) + BasePay
    If Sales >= Quota Then
        Return Total
    Else

        Return BasePay
    End If
End Function

Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click
    Me.Close()
End Sub

Private Sub ClearToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ClearToolStripMenuItem.Click
    TextBox1.Text = ""
    TextBox2.Text = ""
    TextBox3.Text = ""
    TextBox4.Text = ""


End Sub

Private Sub ColorToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ColorToolStripMenuItem.Click
    With ColorDialog1
        ColorDialog1.ShowDialog()
        TextBox2.ForeColor = .Color

    End With
End Sub

Private Sub FontToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FontToolStripMenuItem.Click
    With FontDialog1
        .ShowDialog()
        TextBox2.Font = .font
    End With

End Sub

Private Sub SummaryToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SummaryToolStripMenuItem.Click
    Dim MessageString As String
    MessageString = "Amount of sales: " & TotalSalesAmt.ToString("C") _
    & Environment.NewLine & Environment.NewLine _
    & "Total Commissions: " & TotalComAmt.ToString("C") _
    & Environment.NewLine & Environment.NewLine _
    & "Total Pay: " & GrandTotal.ToString("C")
    MessageBox.Show(MessageString, "Sales Summary", MessageBoxButtons.OK)


End Sub

End Class

Private Sub PayToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PayToolStripMenuItem.Click SalesDecimal = Decimal.Parse(TextBox3.Text) TextBox2.Text = Decimal.Parse(CommissionCalc(SalesDecimal)) ***TextBox4.Text = Decimal.Parse(CommissionRate * SalesDecimal)*** If TextBox3.Text < 1000 Then TextBox4.Text = "" End If TotalSalesAmt += TextBox3.Text TotalComAmt += TextBox4.Text GrandTotal += TextBox2.Text End Sub Private Function CommissionCalc(ByVal Sales As Integer) As Decimal Dim Total = (SalesDecimal * CommissionRate) + BasePay If Sales >= Quota Then Return Total Else Return BasePay End If End Function Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click Me.Close() End Sub Private Sub ClearToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ClearToolStripMenuItem.Click TextBox1.Text = "" TextBox2.Text = "" TextBox3.Text = "" TextBox4.Text = "" End Sub Private Sub ColorToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ColorToolStripMenuItem.Click With ColorDialog1 ColorDialog1.ShowDialog() TextBox2.ForeColor = .Color End With End Sub Private Sub FontToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FontToolStripMenuItem.Click With FontDialog1 .ShowDialog() TextBox2.Font = .font End With End Sub Private Sub SummaryToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SummaryToolStripMenuItem.Click Dim MessageString As String MessageString = "Amount of sales: " & TotalSalesAmt.ToString("C") _ & Environment.NewLine & Environment.NewLine _ & "Total Commissions: " & TotalComAmt.ToString("C") _ & Environment.NewLine & Environment.NewLine _ & "Total Pay: " & GrandTotal.ToString("C") MessageBox.Show(MessageString, "Sales Summary", MessageBoxButtons.OK) End Sub

2 个答案:

答案 0 :(得分:1)

在将变量推入文本框时,需要显式转换变量。

If TextBox3.Text < 1000 Then
    TextBox4.Text = ""
End If
TotalSalesAmt += TextBox3.Text
TotalComAmt += TextBox4.Text
GrandTotal += TextBox2.Text

例如,上面的TextBox3.Text是一个String,而不是Decimal。您尝试移动类型而不将它们转换为适当的类型。

答案 1 :(得分:0)

  

TextBox2.Text = Decimal.Parse(CommissionCalc(SalesDecimal))

您正在使用Decimal.Parse。 Decimal.Parse用于将String解析为Decimal类型,您似乎使用它来执行字符串到十进制和十进制到字符串。如果要获取十进制值的字符串表示形式,则应使用ToString。在上面的例子中形成你的代码,CommisionCalc返回一个十进制数,你需要调用CommissionCalc(SalesDecimal).ToString()而不是Decimal.Parse()