这是我第一次在stackoverflow上发帖,希望能得到一些帮助!
我在VB中为咖啡馆创建一个程序来计算订单的价格。
我有12个按钮,每个按钮都有食物价值和个人价格。单击该按钮时,它会在列表视图中显示数量和小计。例如。如果单击button1(Ham),则在列表视图中将显示button1,0.8(价格),1(数量)和小计(价格*数量)相同的button2等。
当客户选择了他们想要的项目数量时,我希望它在Listview的页脚中显示运行总计。我希望它在单击另一个按钮时自动更改而不单击总按钮.7
我希望我能够很好地解释这个= S
下面按钮的代码(每个按钮具有相同的代码但只有不同的名称)
Private Sub btnHam_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnHam.Click
Me.quantityHam = Me.quantityHam + 1
Dim str(4) As String
Dim item As ListViewItem
str(0) = "Ham, Egg and Chips"
str(1) = 4.5
str(2) = Me.quantityHam.ToString()
str(3) = str(1) * str(2)
Dim WholeString As String = str(0)
item = New ListViewItem(str)
For maindish As Integer = 0 To Me.ListView1.Items.Count - 1
If (Me.ListView1.Items(maindish).ToString = "ListViewItem: {" + WholeString + "}") Then
Me.ListView1.Items.RemoveAt(maindish)
Me.ListView1.Items.Add(item)
Return
End If
Next
Me.ListView1.Items.Add(item)
End Sub
答案 0 :(得分:0)
此解决方案预先假定您的按钮具有单个单词,空格和价格的文本。如果不是这样,你将不得不调整细节。您只需要一个Handles
所有按钮点击事件的单个子:
Private Sub Button_Click(sender As Object, e As EventArgs) Handles Button1.Click, Button2.Click, Button3.Click, Button4.Click, Button5.Click
'append to underlying order
Dim bText As String = TryCast(sender, Button).Text
If order.ContainsKey(bText) Then
order(bText) += 1
Else
order.Add(bText, 1)
End If
'display order to listview
ListView1.Clear()
For Each kvp In order
ListView1.Items.Add(String.Format("{1}{0}{2}{0}{3}", " ", kvp.Key.Split(" ").First, kvp.Value, Convert.ToDouble(kvp.Key.Split(" ").Last) * kvp.Value))
Next
ListView1.Items.Add("Total: " & Aggregate kvp In order Into Sum(Convert.ToDouble(kvp.Key.Split(" ").Last) * kvp.Value))
End Sub
Order是在类顶部声明的字典,如:
Dim order As New Dictionary(Of String, Integer)
这与你的目标一致,对吧?