【问题标题】:Display all makred radio button and check boxs in a msgbox在 msgbox 中显示所有标记的单选按钮和复选框
【发布时间】:2015-10-25 23:05:49
【问题描述】:

选择表单上的按钮时,我必须在MSGBox中显示所有选定的无线电按钮。按钮和框位于几个组框中。我不确定如何编写该代码。

【问题讨论】:

  • WinForms?网络表单? WPF?

标签: vb.net


【解决方案1】:

假设 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

【讨论】:

    猜你喜欢
    • 2018-01-06
    • 1970-01-01
    • 1970-01-01
    • 2020-12-29
    • 1970-01-01
    • 2016-12-07
    • 1970-01-01
    • 1970-01-01
    • 2021-06-02
    相关资源
    最近更新 更多