如何累积恒定金额的运行总计?

时间:2016-02-25 00:09:46

标签: forms visual-studio

我正在为我的课做作业,而且我是编码时的初学者。此作业要求我选择服务和折扣率,然后在两者下面的总文本框中显示它们。

我的问题是,我应该能够选择多个服务并将它们合计在一起,但我无法理解。我将发布以下代码:

Public Class Form1

Dim CurrentServicesDec As Decimal
Dim CurrentServicesTotal As Decimal
Private DiscountServicesDec As Decimal



Private Sub CalculateButton_Click(sender As Object, e As EventArgs) Handles CalculateButton.Click
    'Calculate the Service(s) selected and add the discount if any.

    If MakeOverCheckBox.Checked = True Then
        CurrentServicesDec = 125

    ElseIf Val(HairStylingCheckBox.Checked) = True Then
        CurrentServicesDec = 60

    ElseIf Val(ManicureCheckBox.Checked) = True Then
        CurrentServicesDec = 35

    ElseIf Val(MakeupCheckbox.Checked) = True Then
        CurrentServicesDec = 200
    End If
    If (TenPercentRadio.Checked) = True Then
        DiscountServicesDec = 0.1
    ElseIf TwentyPercentRadio.Checked = True Then
        DiscountServicesDec = 0.2


    End If

    CurrentServicesTotal = CurrentServicesDec - (CurrentServicesDec * DiscountServicesDec)

    txtTotal.Text = FormatCurrency(CurrentServicesTotal)



End Sub

Private Sub ClearButton_Click(sender As Object, e As EventArgs) Handles ClearButton.Click
    'Clear the options on the form.

    txtTotal.Text = ""
    TenPercentRadio.Checked = False
    TwentyPercentRadio.Checked = False

End Sub

Private Sub ExitButton_Click(sender As Object, e As EventArgs) Handles ExitButton.Click
    'Close the program

    Me.Close()
End Sub

Private Sub PrintButton_Click(sender As Object, e As EventArgs) Handles PrintButton.Click
    'Print Preview the Form

    PrintForm1.PrintAction = Printing.PrintAction.PrintToPreview
    PrintForm1.Print()
End Sub
End Class

这就是我所拥有的,只要程序能够选择一项服务并添加折扣,它就能完美运行,但不适用于所选择的多项服务!

提前谢谢。

1 个答案:

答案 0 :(得分:0)

您的程序流有一些问题可以解决您的预期行为,因为If块以If MakeOverCheckBox.Checked = True Then开头,后续分支使用ElseIf会导致它们跳过。

当我认为你应该将它添加到一起时,你也只是分配CurrentServicesDec的值 - 所以在你的Calculate函数中声明变量然后使用+=

因此,要在复选框控件中获取检查倍数的行为,请将ElseIf更改为If块。

Private Sub CalculateButton_Click(sender As Object, e As EventArgs) Handles CalculateButton.Click
Dim CurrentServicesDec As Decimal
If MakeOverCheckBox.Checked = True Then
    CurrentServicesDec += 125
End If

If HairStylingCheckBox.Checked = True Then
    CurrentServicesDec += 60
End If

If ManicureCheckBox.Checked = True Then
    CurrentServicesDec += 35
End If

If MakeupCheckbox.Checked = True Then
    CurrentServicesDec += 200
End If