lblBox使用.ToString(" P0")和.TrimEnd分配lstBox选定项后返回零

时间:2014-04-16 17:58:03

标签: vb.net

我正在编写一个简单的程序,教科书要我在程序中使用string.TrimStart()和string.TrimEnd()方法。您将金额输入到txtSales,然后在lstRates中选择适当的销售税,按计算按钮,它会计算您需要支付多少税,并将其显示在lblTax中。现在,似乎无论我用什么方法做到这一点,lstRates总是返回0 ..有什么建议吗?

Option Explicit On
Option Strict On
Option Infer Off

Public Class frmMain

Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
    Me.Close()
End Sub

Private Sub txtSales_Enter(sender As Object, e As EventArgs) Handles txtSales.Enter
    txtSales.SelectAll()
End Sub

Private Sub lstRates_SelectedValueChanged(sender As Object, e As EventArgs) Handles lstRates.SelectedValueChanged
    lblTax.Text = String.Empty
End Sub

Private Sub txtSales_TextChanged(sender As Object, e As EventArgs) Handles txtSales.TextChanged
    lblTax.Text = String.Empty
End Sub

Private Sub frmMain_Load(sender As Object, e As EventArgs) Handles Me.Load
    ' fills the list box with tax rates

    For dblRate As Double = 0.02 To 0.1 Step 0.01
        lstRates.Items.Add(dblRate.ToString("P0"))
    Next dblRate
    lstRates.SelectedIndex = 0
End Sub

Private Sub btnCalc_Click(sender As Object, e As EventArgs) Handles btnCalc.Click

    Dim dblSales As Double
    Dim dblRates As Double
    Dim strRates As String

    Double.TryParse(txtSales.Text, dblSales)
    strRates = lstRates.Text
    strRates = strRates.TrimEnd("%"c)
    Double.TryParse(strRates, dblRates)
    Double.TryParse(lstRates.Text, dblRates)

    lblTax.Text = (dblSales * dblRates).ToString

End Sub

结束班

1 个答案:

答案 0 :(得分:0)

删除了Double.TryParse(lstRates.Text,dblRates)并添加了dblRates * = 0.01 奇迹般有效!

Private Sub btnCalc_Click(sender As Object, e As EventArgs) Handles btnCalc.Click

    Dim dblSales As Double
    Dim dblRates As Double
    Dim strRates As String

    Double.TryParse(txtSales.Text, dblSales)
    strRates = lstRates.Text
    strRates = strRates.TrimEnd("%"c)
    Double.TryParse(strRates, dblRates)
    dblRates *= 0.01


    lblTax.Text = (dblSales * dblRates).ToString("C2")

End Sub