读书人

递归地按照Name属性查找控件为什么找

发布时间: 2012-02-27 10:00:22 作者: rapoo

递归地按照Name属性查找控件,为什么找不到?
问题是老生常谈了,但是到自己写就傻眼了。

比如查找一个叫做“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 不能要

读书人网 >VB Dotnet

热点推荐