首先,我具有将 a 上的所有控件展平的功能Control
:
Protected Function GetAllControls(Optional ownerControl As Control = Nothing) As IEnumerable(Of Control)
Dim ret = New List(Of Control)()
For Each child As Control In If(ownerControl, Me).Controls
ret.AddRange(GetAllControls(child))
Next
ret.Add(ownerControl)
Return ret
End Function
然后,我想使用以下代码隐藏控件上的某些按钮:
Dim buttons = GetAllControls().Where(Function(c) c.Name.StartsWith("subButton"))
For Each ctrl As Control In buttons
ctrl.Visible = False
Debug.WriteLine("Hid button " & ctrl.Name)
Next
然而,在四个按钮 - 正确的计数 - 被隐藏后,我得到一个NullReferenceException
, VS2012 突出显示了 lambda 表达式。
什么可能导致这种情况?