简体   繁体   中英

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM