我必须使用if / else if语句来添加单选按钮和检查按钮,但我需要这样做,以便当一个人选择它将添加的复选按钮和单选按钮时。
我尝试使用if if then case,但在执行此操作时只会出错。
我的代码:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Calculate.Click
'calculate value of subtotal
Me.Subtotal.Text = Val(Me.CarTextBox.Text)
If Pearlized.Checked = True Then
Me.Subtotal.Text = Val(Me.CarTextBox.Text) + 345.72
ElseIf Customized.Checked = True Then
Me.Subtotal.Text = Val(Me.CarTextBox.Text) + 599.99
ElseIf Stereo.Checked = True Then
Me.Subtotal.Text = Val(Me.CarTextBox.Text) + 425.76
ElseIf Leather.Checked = True Then
Me.Subtotal.Text = Val(Me.CarTextBox.Text) + 987.41
ElseIf Comp.Checked = True Then
Me.Subtotal.Text = Val(Me.CarTextBox.Text) + 1741.23
End If
'calculate tax
Me.Total.Text = Val(Me.Subtotal.Text) * 1.08
'calculate amount due
Me.AmountDue.Text = Val(Me.Total.Text) - Val(TradeTextBox.Text)
End Sub
答案 0 :(得分:0)
在方法的开头设置小计变量。 在测试每个复选框时添加它。 最后执行计算。
我建议您在配置或关联的文本文件中保留珠光,自定义,立体声,税率等的值。当它们发生变化时,它将在未来拯救你。
Private Sub calculateTotal()
Try
'calculate value of subtotal
Dim subTotal As Double = CDbl(CarTextBox.Text)
If Pearlized.Checked = True Then
subTotal += subTotal + 345.72
End If
If Customized.Checked = True Then
subTotal += subTotal + 599.99
End If
If Stereo.Checked = True Then
subTotal += subTotal + 425.76
End If
If Leather.Checked = True Then
subTotal += subTotal + 987.41
End If
If Comp.Checked = True Then
subTotal += subTotal + 1741.23
End If
'calculate tax
Me.SubTotal.Text = subTotal
Me.Total.Text = subTotal * 1.08
'calculate amount due
Me.AmountDue.Text = Val(Me.Total.Text) - Val(TradeTextBox.Text)
Catch ex As Exception
MessageBox.Show(String.Concat("An error occurred: ", ex.Message))
End Try
End Sub