显示msgbox中的所有makred单选按钮和复选框

时间:2015-10-25 23:05:49

标签: vb.net

当选择表单上的按钮时,我必须在msgbox中显示所有选中的单选按钮和复选框。按钮和框位于几个组框中。我不知道如何编写该代码。

1 个答案:

答案 0 :(得分:0)

假设WinForms ...尝试类似:

Public Class Form1

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim results As List(Of String) = GetSelections()
        Dim msg As String = String.Join(Environment.NewLine, results)
        MessageBox.Show(msg)
    End Sub

    Private Function GetSelections() As List(Of String)
        Dim selections As New List(Of String)
        FindSelections(Me, selections)
        Return selections
    End Function

    Private Sub FindSelections(ByVal cont As Control, ByVal results As List(Of String))
        For Each ctl As Control In cont.Controls
            If TypeOf ctl Is RadioButton Then
                Dim rb As RadioButton = DirectCast(ctl, RadioButton)
                If rb.Checked Then
                    results.Add(rb.Text)
                End If
            ElseIf TypeOf ctl Is CheckBox Then
                Dim cb As CheckBox = DirectCast(ctl, CheckBox)
                If cb.Checked Then
                    results.Add(cb.Text)
                End If
            ElseIf ctl.HasChildren Then
                FindSelections(ctl, results)
            End If
        Next
    End Sub

End Class