如何获取窗体的所有TextBox控件 [英] How to get all TextBox controls of a Form

查看:72
本文介绍了如何获取窗体的所有TextBox控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试访问 Form TextBox 控件,该控件位于 GroupBox 内的 Panel 中以及表单上的任何其他 TextBox 控件,但我无法访问它们.这是我尝试过的:

I am trying to access the TextBox controls of a Form which are in a Panel inside a GroupBox as well as any other TextBox controls on the form but I am unable to access them. This is what I have tried:

For Each ctl As Control In Me.Controls
    If ctl.GetType Is GetType(GroupBox) Then
    'now that we have the groupboxes and panels on this form 
    'loop through them and get their textboxes
        Dim ctrl = CType(ctl, Control)
        For Each ctrl In ctl.Controls
            ....

推荐答案

Yo可以创建一种方法来查找控件的所有后代:

Yo can create a method to find all descendants of a control this way:

Private Function GetAllControls(control As Control) As IEnumerable(Of Control)
    Dim controls = control.Controls.Cast(Of Control)()
    Return controls.SelectMany(Function(ctrl) GetAllControls(ctrl)).Concat(controls)
End Function

然后,您可以使用它来查找表单的所有 TextBox 后代:

Then you can use it to find all TextBox descendants of your form:

Dim textboxes = Me.GetAllControls(Me).OfType(Of TextBox)().ToList()
For Each item As TextBox In textboxes
    MessageBox.Show(item.Name)
Next

这篇关于如何获取窗体的所有TextBox控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆