简体   繁体   English

如何检查是否选中了动态创建的复选框(VB.NET)?

[英]How can I check whether dynamically-created Checkboxes are checked (VB.NET)?

I have a start on looping through dynamically-created Checkboxes: 我开始循环遍历动态创建的复选框:

For Each cntrl As Control In Me.Controls
    If TypeOf cntrl Is CheckBox Then
        If (cntrl As CheckBox).Checked Then
            'Do Something
        End If
    End If
Next

...but I don't know what I need instead of this line: ...但是我不知道我需要什么,而不是这条线:

If (cntrl As CheckBox).Checked Then

...which was just a guess and which does not compile. ...这只是猜测,无法编译。

Using LInQ would save you some lines of code: 使用LInQ可以节省一些代码行:

Sub Test()
    Dim myList as New List(Of CheckBox)
    For Each cbox As CheckBox In Me.Controls.OfType(Of CheckBox).Where(Function(cb) cb.Checked)
        myList.Add(cbox)
    Next
    Msgbox(String.Format("{0} checkboxes were checked!", myList.Count))
End Sub

我认为您想做的是:

If DirectCast(cntrl, CheckBox).Checked = True Then

I would do this: 我会这样做:

Dim con As Checkbox
For Each con In Me.Controls
   If con.Checked = True
      'Do Something
   End If
Next

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何删除vb.net中动态创建的控件? - How to delete dynamically-created controls in vb.net? vb.net中一个组框内是否勾选了多个复选框如何勾选显示 - How do I check and display if multiple checkboxes are checked within a group box in vb.net 如何要求在vb.net中选中复选框 - How to require checkboxes be checked in vb.net 如何在 Z6145AFC977C7​​660083BDCDDEA91B7D09Z 中的 Msgbox 中显示唯一“已选中”复选框的文本? - How can I show the text of the only "Checked" checkboxes into a Msgbox in Vb.net? 使用VB.net,我有多个动态生成的复选框,我需要获取它们当前的检查状态 - Using VB.net, I have multiple dynamically generated checkboxes that I need to get their current checked state 如何访问动态创建的复选框并确定选中的 state? - How can I access dynamically created checkboxes and determine checked state? 我如何检查vb.net的datagridview列中的复选框是否已选中 - how can i check if checkbox is checked in datagridview column in vb.net 如何计算在VB.Net中选中了多少个复选框 - How to calculate how many checkboxes are checked in VB.Net 在VB.NET中选中多个复选框时,如何显示复选框的最高平均值? - How do I display the highest average of checkbox when multiple checkboxes checked in VB.NET? 如何将事件添加到通过单击按钮 VB.NET 动态创建的 PictureBox - How can I add events to a PictureBox dynamically created by clicking button VB.NET
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM