我可以遍历我的控件并检索它们分配的属性的名称吗?

Can I loop through my controls and retrieve the name of their assigned properties?

我有一个复选框列表,如果有复选框,我需要获取属性名称 - 或者是否有更有效的方法?下面的代码确保一次只能点击两个复选框,我需要获取这两个复选框的名称。

当前代码:

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

I need to get the name of the two checkboxes

你已经有了循环;使用它:

For Each CheckBox In CheckBoxArray
    If CheckBox.Checked = True Then
        Debug.Print(CheckBox.Name)
    End If
Next