英文:
How do I integrate checkboxes and comboboxes with different values and do a basic math statement using only selection if-else-then-elseif statements?
问题
我的课程作业要求我创建一个表单,其中不同选择的复选框和组合框选项将在按下按钮时添加或减去输入的金额,并将结果输出到标签框中。
我的教授告诉我,解决方案不应该是一个巨大的嵌套if语句块,显示出每种可能的组合,而应该是一个简单的解决方案,如果我们要扩展表单以包含两个以上的复选框,只需扩展几行代码,而不是呈指数增加代码的数量。
教授还表示,我们只能使用if、then和elseif语句。AND/OR/NOT是允许的。不允许使用变量和case语句。算法必须非常简单。
我目前尝试了以下代码片段,目前只有最后一组if-then语句能够工作。在此之前的所有内容都无法正常工作。
Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click
If cboBuilding.Text = "Crashing Waves" Then
lblRentDisplay.Text = txtBaseRent.Text + 210
ElseIf cboBuilding.Text = "SunView" Then
lblRentDisplay.Text = txtBaseRent.Text + 0
End If
If chkLanai.Checked = True Then
lblRentDisplay.Text = txtBaseRent.Text + 65
End If
If chkRooftopDeck.Checked = True Then
lblRentDisplay.Text = txtBaseRent.Text + 120
End If
If chkNonSmoking.Checked = True Then
lblRentDisplay.Text = txtBaseRent.Text - 25
Else
lblRentDisplay.Text = txtBaseRent.Text + 0
End If
End Sub
我甚至将复选框语句嵌套在每个组合框语句下面,但没有成功。
即使创建一个包含所有组合的巨大代码块也没有给我带来结果。
英文:
My class homework is asking me to create a form wherein different selected checkboxes and combobox choices will add or subtract from an inputted amount and output the result to a label box when a button is pressed.
My professor tells me that the solution isn't a giant block of nested if statements that show every combination possible, but rather should be a simple solution where if we were to expand the form with two more checkboxes, it should only take a couple of lines to expand it, and not exponentially increase the amount of code we should have.
The professor also states that we are only limited to using if, then, and elseif statements. AND/OR/NOT are allowed. Variables and cases are not allowed. The algorithm has to be very simple.
I have currently tried this code snippet, which currently makes only the last set of if-then statements to work. Everything before that does not work.
Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click
If cboBuilding.Text = "Crashing Waves" Then
lblRentDisplay.Text = txtBaseRent.Text + 210
ElseIf cboBuilding.Text = "SunView" Then
lblRentDisplay.Text = txtBaseRent.Text + 0
End If
If chkLanai.Checked = True Then
lblRentDisplay.Text = txtBaseRent.Text + 65
End If
If chkRooftopDeck.Checked = True Then
lblRentDisplay.Text = txtBaseRent.Text + 120
End If
If chkNonSmoking.Checked = True Then
lblRentDisplay.Text = txtBaseRent.Text - 25
Else
lblRentDisplay.Text = txtBaseRent.Text + 0
End If
I have even nested the checkbox statements under each of the combo box statements and have had no luck.
Even creating a giant block of every combination yielded me no results.
答案1
得分: 1
首先,打开 Option Strict
(或 Option Infer
)! 起初可能会感觉需要额外工作,但当你习惯后,这确实会让你更快速、更准确。
顺便说一下,所有控件都有一个 Tag
属性<sup>*</sup>。使用这个属性来存储每个控件的租金调整值。然后按照以下方式操作:
Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click
Static perkMap As New Dictionary(Of String, Integer) From {
{ "Crashing Waves", 120},
{ "SunView", 0}
}
Dim perkPrice As Integer = 0
If perkMap.ContainsKey(cboBuilding.Text) Then
perkPrice = perkMap(cboBuilding.Text)
End If
Dim adjustmentPrice As Integer =
Me.Controls.OfType(Of CheckBox)().
Where(Function(cb) cb.Checked).
Select(Function(cb) CInt(cb.Tag)).
Sum()
lblRentDisplay.Text = (CInt(txtBaseRent.Text) + perkPrice + adjustmentPrice).ToString()
End Sub
但考虑到这看起来像是你还没有学会如何使用 lambda 表达式方法的作业类型,你可能更喜欢这样编写:
Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click
Static perkMap As New Dictionary(Of String, Integer) From {
{ "Crashing Waves", 120},
{ "SunView", 0}
}
Dim perkPrice As Integer = 0
If perkMap.ContainsKey(cboBuilding.Text) Then
perkPrice = perkMap(cboBuilding.Text)
End If
Dim adjustmentPrice As Integer = 0
Dim checkboxes = Me.Controls.OfType(Of CheckBox)()
For Each box as CheckBox In checkboxes
If box.Checked Then
adjustmentPrice += CInt(box.Tag)
End If
Next
lblRentDisplay.Text = (CInt(txtBaseRent.Text) + perkPrice + adjustmentPrice).ToString()
End Sub
除了 Tag
属性之外,你还可以使用类似于 perkMap
的字典来存储租金调整,根据每个复选框的 Name
属性查找值。
如果你还没有遇到泛型并且不熟悉使用 OfType(Of T)
,你可以创建一个数组并在方法开始时将复选框添加到其中。然后,当你添加复选框时,唯一需要调整的代码是将其添加到数组中。
最后,有可能你的窗体上还有其他不属于此组的复选框。在这种情况下,将所有这些复选框放在一个公共容器控件中,例如 Panel
、GroupBox
或 FlowLayoutPanel
。然后,在调用 OfType(Of T)
方法时,可以使用该父容器而不是 Me
。
<sub>* 顺便说一句:我希望看到一个更新的 WinForms,其中包含一组使用泛型类型的控件来处理这个属性。</sub>
英文:
First of all, turn on Option Strict
(or Option Infer
)! I know it seems like extra work at first, but this really will make you faster and more accurate as you get used to it.
That out of the way, all controls have a Tag
property<sup>*</sup>. Use this property to store the rent adjustment value for each control. Then do this:
Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click
Static perkMap As New Dictionary(Of String, Integer) From {
{ "Crashing Waves", 120},
{ "SunView", 0}
}
Dim perkPrice As Integer = 0
If perkMap.ContainsKey(cboBuilding.Text) Then
perkPrice = perkMap(cboBuilding.Text)
End If
Dim adjustmentPrice As Integer =
Me.Controls.OfType(Of CheckBox)().
Where(Function(cb) cb.Checked).
Select(Function(cb) CInt(cb.Tag)).
Sum()
lblRentDisplay.Text = (CInt(txtBaseRent.Text) + perkPrice + adjustmentPrice).ToString()
End Sub
But given this looks like the type of homework where you haven't learned how to use those lambda expression methods yet, you might prefer to write it like this:
Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click
Static perkMap As New Dictionary(Of String, Integer) From {
{ "Crashing Waves", 120},
{ "SunView", 0}
}
Dim perkPrice As Integer = 0
If perkMap.ContainsKey(cboBuilding.Text) Then
perkPrice = perkMap(cboBuilding.Text)
End If
Dim adjustmentPrice As Integer = 0
Dim checkboxes = Me.Controls.OfType(Of CheckBox)()
For Each box as CheckBox In checkboxes
If box.Checked Then
adjustmentPrice += CInt(box.Tag)
End If
Next
lblRentDisplay.Text = (CInt(txtBaseRent.Text) + perkPrice + adjustmentPrice).ToString()
End Sub
Separate from the Tag
property you could also instead a Dictionary similar to perkMap
to store the rent adjustments, where you lookup the value based on each CheckBox's Name
property.
If you haven't encountered generics yet and aren't comfortable using OfType(Of T)
, you can make an array and add the checkboxes to it at the beginning of the method. Then the only code adjustment needed when you add a checkbox is to also add it to the array.
Finally, it's possible you have other checkboxes on the form that are not part of this group. Of course you don't want to use those here. In that situation, put all these checkboxes in a common container control, like a Panel
, GroupBox
, or FlowLayoutPanel
. Then you can use that parent instead of Me
when calling the OfType(Of T)
method.
<sub>* Aside: I'd love to see an updated WinForms with a set of controls using a generic type for this property.</sub>
答案2
得分: 0
您每次都从基础值重新开始明确计算每个修改。只有第一次计算应该使用"基本租金"。随后的计算应该只是添加或从lblRentDisplay
中已有的值中减去:
If cboBuilding.Text = "Crashing Waves" Then
lblRentDisplay.Text = txtBaseRent.Text + 210
End If
If chkLanai.Checked = True Then
lblRentDisplay.Text = lblRentDisplay.Text + 65
End If
If chkRooftopDeck.Checked = True Then
lblRentDisplay.Text = lblRentDisplay.Text + 120
End If
If chkNonSmoking.Checked = True Then
lblRentDisplay.Text = lblRentDisplay.Text - 25
End If
*注意,使用.Text
属性进行数学计算应该极力不推荐!理想情况下,您应该声明一些保存数值的类级别变量,并使用这些变量进行数学计算。然后,在计算出的值后只需更新标签。
英文:
You're explicitly calculating each modification by starting over from the base value every time. Only the first calculation should use the "base rent". Each subsequent one should simply add or remove from what is already in lblRentDisplay
:
If cboBuilding.Text = "Crashing Waves" Then
lblRentDisplay.Text = txtBaseRent.Text + 210
End If
If chkLanai.Checked = True Then
lblRentDisplay.Text = lblRentDisplay.Text + 65
End If
If chkRooftopDeck.Checked = True Then
lblRentDisplay.Text = lblRentDisplay.Text + 120
End If
If chkNonSmoking.Checked = True Then
lblRentDisplay.Text = lblRentDisplay.Text - 25
End If
*Caveat, using the .Text
property to do math should be HIGHLY DISCOURAGED! Ideally you'd declare some class level variables that hold NUMERIC values and do math with those instead. Then simply update the label at the end with the calculated value.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论