简体   繁体   English

检查x次数的复选框后,请执行x

[英]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.

相关问题 限制动态填充的 CheckBoxList 中选中的 CheckBox 数 - Limit Number of Checked CheckBoxes in Dynamically Populated CheckBoxList 在VB.NET中选中多个复选框时,如何显示复选框的最高平均值? - How do I display the highest average of checkbox when multiple checkboxes checked in VB.NET? 选中2个复选框时,将文本放入文本框 - putting text in a textbox when 2 checkboxes are checked VB-如果仅选中一定数量的复选框,则运行if语句? - VB - Running an if statement if only a certain number of checkboxes are checked? 如何在GridView中对复选框进行分组,以便在选中组中的所有复选框时可以显示一条消息 - How to group Checkboxes in a GridView so I can display a message when all CheckBoxes in a group are checked 将复选框定义为已在复选框列表中选中 - define checkboxes as checked in checkboxlist 搜索表带有x个参数 - Search table with x number of arguments 根据datagridview VB.NET 2010上已选中复选框的数量,通过执行LOOP来保存多次 - Save multiple times by doing a LOOP depending on the number of checked checkboxes on a datagridview VB.NET 2010 当x是字符串并且我不了解T的属性时,如何按属性x对(T)列表进行排序 - How do I sort a list(of T) by a property x when x is a string and I don't know about the properties on T vb.net中一个组框内是否勾选了多个复选框如何勾选显示 - How do I check and display if multiple checkboxes are checked within a group box in vb.net
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM