问题是老生常谈了,但是到自己写就傻眼了。
比如查找一个叫做“txtAAA”的文本框,txtAAAz在GroupBox1里,容器间的关系是这样的:
Form---〉TabControl---〉TabPage---〉GroupBox1,按照搜索引擎的思路,写了一个函数:
- VB.NET code
Private Function FindControlRecursive(ByVal iCon As Control, ByVal iName As String) If iCon.Name = iName Then Return iCon End If For Each iChild As Control In iCon.Controls Dim foundcontrol As Control = FindControlRecursive(iChild, iName) If foundcontrol IsNot Nothing Then Return foundcontrol MsgBox("!") End If Next Return Nothing End Function
试验了一下,最后返回的还是Nothing,请问这个函数哪里需要改正?
谢谢大家的回答!
------解决方案--------------------
你都Return Nothing,肯定返回Nothing
------解决方案--------------------
Debug.Print(FindControlRecursive(Me, Me.CheckBox1.Name).Name)
没有问题
------解决方案--------------------
搞错,
return nothing 不能要