2016-11-23 47 views
0

根据列表框中的项目计算预税,税金和总金额。下面是执行时它目前的样子:使用列表框计算价格

https://drive.google.com/open?id=0B3W0gSES_-fNMmdnU1VTSzR2dFk

这里是我到目前为止的代码:

'load ListBox with test data: 
    For dblPrices As Double = 1 To 4 
     lstPrices.Items.Add(dblPrices.ToString("c2")) 
    Next dblPrices 

    'calculate pretax total: 

    Dim dblPretaxTotal As Double = 0 
    Dim dblSelectedPrice As Double 

    For intTax As Integer = 0 To lstPrices.Items.Count - 1 
     lstPrices.SelectedIndex = 0 
     Dim strPrice As String 
     strPrice = Convert.ToString(lstPrices.SelectedItem) 
     Double.TryParse(strPrice, dblSelectedPrice) 
     dblPretaxTotal = dblSelectedPrice 
    Next intTax 

我只有它编程钙质,目前显示的税前总。它应该显示$ 10.00。欢迎任何建议。

+0

你没有问过问题,但如果你想总结一下,你需要积累'dblTotal + = dblSelectedPrice'这个循环没有读,它只是每次将'dblPretaxTotal'改为一个新值。使用调试器来解决这类问题。多数民众赞成在 – Plutonix

+0

谢谢,我会检查出来! – htmlbran86

+0

我想回答这个问题,你可以让它更详细吗?你想要发生什么? –

回答

0

我认为你的问题是失败的双重转换。

试试这个:

dblPretaxTotal += Double.Parse(strPrice, NumberStyles.Currency) * taxRate; 

代替Double.TryParse()不检查的结果。 NumberStyles.Currency和税率也不见了。