如何验证组框中至少一个单选按钮被选中?我正在验证所有文本控件是否像这样正确地填充;
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对于单选按钮是否有类似的方法,因为我似乎找不到一种合乎逻辑的方法来完成这一任务。
发布于 2014-10-17 11:51:09
您可以使用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 Ifhttps://stackoverflow.com/questions/26424379
复制相似问题