【问题标题】:Can I loop through my controls and retrieve the name of their assigned properties?我可以遍历我的控件并检索它们分配的属性的名称吗?
【发布时间】:2015-01-02 13:43:51
【问题描述】:

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

当前代码:

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

【问题讨论】:

  • 您已经提取了未选中的复选框列表,提取选中的复选框列表应该是小菜一碟。顺便说一句,处理程序错过了 chckSep
  • 一对组合框更适合你描述的用例

标签: vb.net properties controls


【解决方案1】:

我需要得到两个复选框的名称

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-06-11
    • 1970-01-01
    • 2014-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-18
    相关资源
    最近更新 更多