0
首先,我有函數用於平在Control
所有控件:這個LINQ代碼有什麼問題?
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表達式。
什麼可能導致此?
謝謝!那會花掉我一段時間,看錯了地方等。 – ProfK