vb.net 在 Visual Basic 中使用带有多个单选按钮的 if 语句计算总和
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21816341/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using if statements with multiple radio buttons to calculate a sum in visual basic
提问by Stick in the Mud
I am using Visual Basic 2010 and trying to calculate an Decimal amount using radio buttons.
我正在使用 Visual Basic 2010 并尝试使用单选按钮计算 Decimal 数量。
The first radio buttons are declared constants representing the cost of a service.
第一个单选按钮被声明为代表服务成本的常量。
The second set of radio buttons are for a discount amount.
第二组单选按钮用于折扣金额。
The issue I am having is the value returning, no matter what discount button is selected is the value for the first if statement.
我遇到的问题是返回的值,无论选择哪个折扣按钮都是第一个 if 语句的值。
I feel like I should be using a case statement, unfortunately the book I am using is vague concerning case statements.
我觉得我应该使用案例陈述,不幸的是,我正在使用的这本书关于案例陈述的内容含糊不清。
Thank you for taking the time to read my question.
感谢您花时间阅读我的问题。
If MakeoverRadioButton1.Checked + Discount10RadioButton5.Checked Then
CostSelectedService1 = Makeover_Decimal - (Makeover_Decimal * 0.1)
AmountSelectedServiceLabel3.Text = CostSelectedService1.ToString("C")
ElseIf MakeoverRadioButton1.Checked + Discount20RadioButton6.Checked Then
CostSelectedService2 = Makeover_Decimal - (Makeover_Decimal * 0.2)
AmountSelectedServiceLabel3.Text = CostSelectedService2.ToString("C")
ElseIf MakeoverRadioButton1.Checked + NoDiscountRadioButton7.Checked Then
CostSelectedService = Makeover_Decimal
AmountSelectedServiceLabel3.Text = CostSelectedService.ToString("C")
End If
回答by Neolisk
You probably meant this:
你可能是这个意思:
If MakeoverRadioButton1.Checked AndAlso Discount10RadioButton5.Checked Then
As a side note, it seems like your cost and discounts are irrelevant to each other. In this case you should be able to cut it with two separate If...ElseIfor Select Case. Otherwise you are looking for n*mIfstatements, which is hard to maintain.
作为旁注,您的成本和折扣似乎彼此无关。在这种情况下,您应该能够用两个单独的If...ElseIf或Select Case. 否则,您正在寻找难以维护的n*mIf语句。
If you want a more specific advice, and a code sample, please elaborate in your question on which radio buttons you currently have and how it's supposed to work (include a screenshot).
如果您想要更具体的建议和代码示例,请在您的问题中详细说明您当前拥有哪些单选按钮以及它应该如何工作(包括屏幕截图)。
回答by ??ssa P?ngj?rdenlarp
Simplified:
简化:
' the default
CostSelectedService = Makeover_Decimal
AmountSelectedServiceLabel3.Text = CostSelectedService.ToString("C")
If MakeoverRadioButton1.Checked AndAlso Discount10RadioButton5.Checked Then
CostSelectedService1 -= (Makeover_Decimal * 0.1)
ElseIf MakeoverRadioButton1.Checked AndAlso Discount20RadioButton6.Checked Then
CostSelectedService2 -= (Makeover_Decimal * 0.2)
End If

