[英]When x number of checkboxes are checked do x to the rest
Hello everyone so I'm having trouble figuring out how I could check if 3 checkboxes inside the GroupBox called "Toppings" are checked.大家好,我无法弄清楚如何检查 GroupBox 中名为“Toppings”的 3 个复选框是否被选中。 I'll have to make it so it adds a certain amount after that to the total when a different size pizza is checked but that I could try to figure out myself.
我将不得不这样做,以便在检查不同尺寸的比萨时,它会在总数中增加一定数量,但我可以尝试弄清楚自己。 I figured out how I could make it work but it's not universal so all the checkboxes could use it.
我想出了如何让它工作,但它不是通用的,所以所有的复选框都可以使用它。
If CheckBox1.Checked = True Then
lblTest.Text = 1
Else
If lblTest.Text <= 0 Then
lblTest.Text = 0
Else
lblTest.Text -= lblTest.Text
End If
End If
The use of the label is just for testing purposes to see if I could get it to 3 and back to 0 when checking and unchecking.标签的使用仅用于测试目的,看看我是否可以在检查和取消检查时将其设置为 3 并返回到 0。 I would like to get a method going where it checks for the change in the checkbox and adds 1 to a variable called TopNum.
我想得到一个方法,它检查复选框中的更改并将 1 添加到名为 TopNum 的变量。 This is so when it reaches 3 it turns on a bool called fTop so every other checkbox after that gets added to the total.
因此,当它达到 3 时,它会打开一个名为 fTop 的布尔值,因此之后的所有其他复选框都会添加到总数中。 This could then help me figure out how I could apply it to the pizza size giving it different prices amounts on the toppings by adding a multiplier.
然后,这可以帮助我弄清楚如何将它应用于比萨大小,通过添加乘数来赋予它不同的浇头价格。
Given a GroupBox named Toppings
and an Integer field named TopNum
, I would do this:给定一个名为分组框中
Toppings
和一个名为整型字段TopNum
,我这样做:
TopNum = Toppings.Controls.OfType(Of CheckBox)().Count(Function(cb) cb.Checked)
If TopNum >= 3 Then
'...
End If
You can put that in the CheckChanged
event handler for all of the checkboxes.您可以将其放在所有复选框的
CheckChanged
事件处理程序中。 Or better, all of the checkboxes can have the same event handler, which has this code.或者更好的是,所有复选框都可以具有相同的事件处理程序,其中包含此代码。
Also, I'd probably remove the If
conditional and change TopNum
from a field to a property.另外,我可能会删除
If
条件并将TopNum
从字段更改为属性。 Then the If
conditional would go in the property setter.然后
If
条件将进入属性设置器。
Public Property TopNum As Integer
Get
Return _topNum
End Get
Set (value As Integer)
_topNumn = value
If value >= 3 Then
'...
Else
'...
End If
End Set
End Property
Private _topNum As Integer = 0
Public Sub ToppingsChanged(sender As Object, e As EventArgs)
TopNum = Toppings.Controls.OfType(Of CheckBox)().Count(Function(cb) cb.Checked)
End Sub
Public Sub New()
IntializeComponent()
For Each cb As CheckBox In Me.Controls.OfType(Of CheckBox)()
AddHandler cb, AddressOf ToppingsChanged
Next
End Sub
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.