验证单选按钮组

时间:2014-10-17 11:49:08

标签: vb.net validation

如何验证组框中至少有一个单选按钮是否已被选中?我正在验证所有文本控件都是这样正确填充的;

        For Each ctrl As Control In Me.Controls
        If TypeOf ctrl Is TextBox Then
            If ctrl.Text = "" Then
                MessageBox.Show("Please enter information in " & ctrl.Name, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
                Exit Sub
            End If
        End If

是否有类似的单选按钮方法,因为我似乎无法找到完成此操作的合理方法。

1 个答案:

答案 0 :(得分:0)

您可以使用LINQ

Dim uncheckedRadios = From radio In Me.groupbox1.Controls.OfType(Of RadioButton)()
                      Where Not radio.Checked
                      Select radio.Name
Dim anyUnchecked As Boolean = uncheckedRadios.Any()

If anyUnchecked Then
    Dim uncheckedNames = String.Join(",", uncheckedRadios)
    MessageBox.Show("Please check all radio-buttons, these are not checked: " & uncheckedNames, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
    Return
End If