如何在VB.net中添加所有变量?我做了指示,将其放在外面来增加它。现在,无论何时我按下按钮,输出都仅为95,并且每当我多次单击图片框上的增量时,输出都与上次相同。真的什么都没发生
Private BibimbapQuantity = 1
Private BulgogiQuantity = 1
Private BibimbapPrice As Integer = 45 * BibimbapQuantity
Private BulgogiPrice As Integer = 50 * BulgogiQuantity
Private TotalPriceInt As Integer = BibimbapPrice + BulgogiPrice
Private Sub PictureBox2_Click(sender As Object, e As EventArgs) Handles buttonBibimbap.Click
BibimbapQuantity += 1
Me.DataGridView2.Rows.Add("Bibimbap", "1", BibimbapPrice)
End Sub
Private Sub buttonBulgogi_Click(sender As Object, e As EventArgs) Handles buttonBulgogi.Click
BulgogiQuantity += 1
Me.DataGridView2.Rows.Add("Bulgogi", "1", BulgogiPrice)
End Sub
Private Sub totalPrice_Click(sender As Object, e As EventArgs) Handles totalPrice.Click
End Sub
Private Sub Button1_Click_1(sender As Object, e As EventArgs) Handles Button1.Click
totalPrice.Text = TotalPriceInt
End Sub
答案 0 :(得分:1)
您可能已经在顶部编写了公式,但只执行了一次。您需要每次执行公式。
Private Sub PictureBox2_Click(sender As Object, e As EventArgs) Handles buttonBibimbap.Click
BibimbapQuantity += 1
BibimbapPrice = 45 * BibimbapQuantity ' <------
Me.DataGridView2.Rows.Add("Bibimbap", "1", BibimbapPrice)
End Sub
您需要对总数进行相同的操作。
完成后,将您的代码放入https://codereview.stackexchange.com/中,它们将对您的重构有很大帮助。 仅在代码实际运行时在codereview上发帖。它们将帮助您找到更好的逻辑编写方法并发现可能的问题。很好的学习机会。