vb.net – 遍历表单中的所有文本框,包括组框内的文本框

vb.net – 遍历表单中的所有文本框,包括组框内的文本框,第1张

概述我在winform中有几个文本框,其中一些在groupbox中.我试图遍历我的表单中的所有文本框: For Each c As Control In Me.Controls If c.GetType Is GetType(TextBox) Then ' Do something End IfNext 但它似乎跳过了groupbox中的那些并且仅循环到表单的其他文本 我在winform中有几个文本框,其中一些在groupBox中.我试图遍历我的表单中的所有文本框:

For Each c As Control In Me.Controls    If c.GetType Is GetType(TextBox) Then        ' Do something    End IfNext

但它似乎跳过了groupBox中的那些并且仅循环到表单的其他文本框.所以我为groupBox文本框添加了另一个For Each循环:

For Each c As Control In GroupBox1.Controls    If c.GetType Is GetType(TextBox) Then        ' Do something    End IfNext

我想知道:有没有办法循环遍历表单中的所有文本框 – 包括组框内的文本框 – 只有一个For Each循环?或者更好/更优雅的方式来做到这一点?

提前致谢.

解决方法 你可以使用这个功能,linq可能是一个更优雅的方式.

Dim allTxt As New List(Of Control)For Each txt As TextBox In FindControlRecursive(allTxt,Me,GetType(TextBox))   '....'NextPublic Shared Function FindControlRecursive(ByVal List As List(Of Control),ByVal parent As Control,ByVal ctrlType As System.Type) As List(Of Control)    If parent Is nothing Then Return List    If parent.GetType Is ctrlType Then        List.Add(parent)    End If    For Each child As Control In parent.Controls        FindControlRecursive(List,child,ctrlType)    Next    Return ListEnd Function
总结

以上是内存溢出为你收集整理的vb.net – 遍历表单中的所有文本框,包括组框内的文本框全部内容,希望文章能够帮你解决vb.net – 遍历表单中的所有文本框,包括组框内的文本框所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/1253229.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-06-07
下一篇2022-06-07

发表评论

登录后才能评论

评论列表(0条)

    保存