Vb设置要在文本框中显示的价格时,它不会显示正确的值

时间:2016-03-05 19:59:33

标签: vb.net visual-studio textbox

我正在制作披萨订购系统,我已将价格值设置为0,以便我可以在以后添加其他值。因此当我尝试让它显示价格时,因为用户在文本框中选择了他们的项目由于某种原因是没有显示正确的价格

Public Class Form1
    Public price As Double
    Public Psmall As Double = 4.0
    Public Pmedium As Double = 7.0
    Public Plarge As Double = 9.0
    Public Pcrusts As Double = 2.0
    Public Pside As Double = 2.5
    Public Pdesert As Double = 2.5
    Public Pdrinks As Double = 1.0
    Public Pizzas As Double = 4.0



    Private Sub Psize_SelectedIndexChanged(sender As Object, e As EventArgs) Handles Psize.SelectedIndexChanged

        For Each Item As Object In Psize.SelectedItem
            orders.AppendText(Item.ToString)
            If Psize.SelectedIndex = 0 Then
                price += 4
            ElseIf Psize.SelectedIndex = 1 Then
                price += 7 * 1
            ElseIf Psize.SelectedIndex = 2 Then
                price += 9 * 1

            End If

            TextBox1.Text = String.Format("{0:C}", price)

        Next

    End Sub

    Private Sub Pcrust_SelectedIndexChanged(sender As Object, e As EventArgs) Handles Pcrust.SelectedIndexChanged
        For Each Item As Object In Pcrust.SelectedItem
            orders.AppendText(Item.ToString)
            price += Pcrusts


        Next

    End Sub

    Private Sub Pizza_SelectedIndexChanged(sender As Object, e As EventArgs) Handles Pizza.SelectedIndexChanged
        For Each Item As Object In Pizza.SelectedItem
            orders.AppendText(Item.ToString)
            price += Pizzas
        Next

    End Sub

    Private Sub Oside_SelectedIndexChanged(sender As Object, e As EventArgs) Handles Oside.SelectedIndexChanged
        For Each Item As Object In Oside.SelectedItem
            orders.AppendText(Item.ToString)
            price += Pside
        Next

    End Sub

    Private Sub Odesert_SelectedIndexChanged(sender As Object, e As EventArgs) Handles Odesert.SelectedIndexChanged
        For Each Item As Object In Odesert.SelectedItem
            orders.AppendText(Item.ToString)
            price += Pdesert
        Next

    End Sub

    Private Sub Pdrink_SelectedIndexChanged(sender As Object, e As EventArgs) Handles Pdrink.SelectedIndexChanged
        For Each Item As Object In Pdrink.SelectedItem
            orders.AppendText(Item.ToString)
            price += Pdrinks
        Next


    End Sub

    Private Sub orders_TextChanged(sender As Object, e As EventArgs) Handles orders.TextChanged

    End Sub


    Private Sub tcost_TextChanged(sender As Object, e As EventArgs)

    End Sub

    Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged

    End Sub
End Class

上面是代码,我尝试使用原始数字而不是我已经声明的变量而且都没有工作

以下是代码在执行时如何在表单中显示价格的链接 http://prntscr.com/abmj0a

2 个答案:

答案 0 :(得分:0)

我认为问题是每次用户选择组合框中的内容时都会增加价格

因此,如果用户首先选择一个小披萨,但随后将其更改为中型披萨,则应设置:

prize+=4 'Now the price is at 4
prize+=7 'Now the price is at 11 (4 before and now +7)

所以最终价格将是11。

我的解决方案是: 为每个组合框声明价格变量,例如price_size,price_drink等。

现在,当用户更改大小Combobox的选择时,您只需说price_size = 4

当他改变饮料组合框时,你说price_drink = 1

最后你宣布result_price = price_size + price_drink + ...

答案 1 :(得分:0)

删除代码中的每个For Each循环。他们完全不必要,很可能是造成问题的原因。

你正试图循环一些技术上不存在的东西。

For Each Item As Object In Psize.SelectedItem
'SelectedItem is just one single item. There's nothing to loop through!

您还应该为price变量提供初始值,以避免空引用异常。

Public price As Double = 0.0

最后,按照julien-100000的说法并声明一个单独的变量,保存每个ComboBox / ListBox的价格选择。