我有一个复选框列表,我需要获取属性名称,如果复选框 - 或者是否有更有效的方法来执行此操作?下面的代码确保一次只能点击两个复选框,我需要获取两个复选框的名称。
当前代码:
Private Sub cb_CheckedChanged(sender As Object, e As EventArgs) Handles chckJan.
CheckedChanged,
chckFeb.CheckedChanged,
chckMar.CheckedChanged,
chckApr.CheckedChanged,
chckMay.CheckedChanged,
chckJun.CheckedChanged,
chckJul.CheckedChanged,
chckAug.CheckedChanged,
chckOct.CheckedChanged,
chckNov.CheckedChanged,
chckDec.CheckedChanged
'get all checkboxes
Dim Months = Controls.OfType(Of CheckBox)().ToArray()
'Get the number of checked CheckBoxes.
Dim checkedBoxCount = Months.Count(Function(cb) cb.Checked)
'Unchecked CheckBoxes should be enabled if and only if the number of checked CheckBoxes is less than the maximum number allowed.
Dim enableUncheckedBoxes = checkedBoxCount < 2
'Get the unchecked CheckBoxes.
Dim uncheckedBoxes = Months.Where(Function(cb) Not cb.Checked)
'Enable or disable the unchecked CheckBoxes as appropriate.
For Each uncheckedBox In uncheckedBoxes
uncheckedBox.Enabled = enableUncheckedBoxes
Next
Dim CheckBoxArray As CheckBox() = {chckJan, chckFeb, chckMar, chckApr, chckMay, chckJun, chckJul, chckAug, chckSep, chckOct, chckNov, chckDec}
For Each CheckBox In CheckBoxArray
If CheckBox.Checked = True Then
End If
Next
End Sub
答案 0 :(得分:0)
我需要获取两个复选框的名称
你已经有了一个循环;使用它:
For Each CheckBox In CheckBoxArray
If CheckBox.Checked = True Then
Debug.Print(CheckBox.Name)
End If
Next