Vb.net 如何检查是否检查了任何单选按钮以更改其前景色
我在表单上的10多个单选按钮上使用“For”语句时遇到问题 一个单选按钮的示例:Vb.net 如何检查是否检查了任何单选按钮以更改其前景色,vb.net,Vb.net,我在表单上的10多个单选按钮上使用“For”语句时遇到问题 一个单选按钮的示例: If Form2.RadioButton1.Checked = True Then Form2.RadioButton1.ForeColor = Color.Red Else Form2.RadioButton1.ForeColor = Color.Yellow End If 但是,如果我想在表单上的任何单选按钮上使用此选项,我将使用以下内容: Dim i As Integer For i = 1
If Form2.RadioButton1.Checked = True Then
Form2.RadioButton1.ForeColor = Color.Red
Else
Form2.RadioButton1.ForeColor = Color.Yellow
End If
但是,如果我想在表单上的任何单选按钮上使用此选项,我将使用以下内容:
Dim i As Integer
For i = 1 To 10
If Form2.RadioButton(i).Checked = True Then
Form2.RadioButton(i).ForeColor = Color.Red
Else
Form2.RadioButton(i).ForeColor = Color.Yellow
End If
Next
.net没有控件数组。迭代窗体的.controls集合并检查控件类型
For Each tControl as Control in Me.Controls
If tControl.GetType Is GetType(RadioButton)
If DirectCast(tControl, RadioButton).Checked
' Change the control's color.
Else
' Change the control's color.
End If
End If
Next
希望这个链接能对你有所帮助。是的,这只在第一次加载表单时有效,但是如果我想从一开始就一直检查表单,是否检查了任何单选按钮,如果是,则立即更改其颜色。在窗体上添加处理程序,然后处理CheckChanged(?可能是单击)事件