vb.net 计算并获取VB.NET中选中复选框的所有值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29277619/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 18:55:04  来源:igfitidea点击:

Count and get all the value of checked checkboxes in VB.NET

vb.netvisual-studio-2010checkbox

提问by bernzkie

lets say i have a form, and under my form i have a panel, and under panel i have a groupbox, and under the groupbox i have another panel, and under of this panel i have a multiple checkboxes, now, how can i count how many checkboxes are checked and how can i get the value of checked checkboxes and put it in arraylist. i have a code but doesnt work.

假设我有一个表单,在我的表单下有一个面板,在面板下我有一个组框,在组框下我有另一个面板,在这个面板下我有多个复选框,现在,我如何计算选中了多少复选框以及如何获取选中复选框的值并将其放入数组列表中。我有一个代码但不起作用。

my code:

我的代码:

        Dim list As New ArrayList
        Dim count As Integer
        count = 0
        If TypeOf element Is CheckBox Then

         If cb.Checked Then
            list.Add(cb.Text)
        'End If
        Else
            For Each childElement In element.Controls
               count += 1
            Next
        End If
        MsgBox(count)
        MsgBox(list)

thank you very much! any help will appreciate. sorry for y bad English.

非常感谢!任何帮助将不胜感激。对不起你的英语不好。

回答by Blackwood

Loop through all the controls in the inner panel and check to see if they are CheckBoxes. If they are, and they are checked, increment the count and add the text to the list. I would use a generic List(Of String) rather than an ArrayList.

循环遍历内部面板中的所有控件并检查它们是否为 CheckBox。如果是,并且已选中,则增加计数并将文本添加到列表中。我会使用通用 List(Of String) 而不是 ArrayList。

Dim count As Integer 
Dim myList As New List(Of String)
For Each cb As CheckBox In panel1.Controls.OfType(Of CheckBox)
    If cb.Checked Then
        count += 1
        myList.Add(cb.Text)
    End If
Next
MessageBox.Show(count.ToString)
MessageBox.Show(String.Join(", ", myList))

[Edit] Code was simplified, as suggested by Plutonix, to use Controls.OfType(Of CheckBox)to loop through only the controls that are of type CheckBox,

[编辑] 按照 Plutonix 的建议,代码被简化,Controls.OfType(Of CheckBox)仅用于循环检查类型为 CheckBox 的控件,

回答by Polar

you can do it using recursive. try this.

你可以使用递归来做到这一点。尝试这个。

        Private Sub getcheckme(ByVal element As Control)
        Dim count As Integer
        count = 0
        If TypeOf element Is CheckBox Then
            If DirectCast(element, CheckBox).Checked = True Then
                count += 1 'this will count the checked checkboxes
                list.Add(element.Text) ' this will add the value of checkboxes into arraylist
            End If
        Else
            For Each childElement In element.Controls
                Me.getcheckme(childElement)
            Next
        End If
        End Sub

just call it using: Me.getcheckme(Me)

只需使用以下方法调用它: Me.getcheckme(Me)

回答by bbakiu

Dim chk As CheckBox
For Each c As Control In thePanel.Controls

    if Typeof c is CheckBox then
     count += 1 // to count check boxes
        chk  =  Ctype(c, Checkbox)
        if chk.Checked Then
            list.Add(chk.Text) // to add the text of checkbox to array
        End If
    End If
Next