如何获取Form的所有TextBox控件

时间:2015-11-21 19:33:52

标签: .net vb.net winforms

我正在尝试访问位于TextBoxForm内的Panel的{​​{1}}控件以及任何其他GroupBox控件表格,但我无法访问它们。这就是我的尝试:

TextBox

1 个答案:

答案 0 :(得分:2)

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

<!-- These do not need to be paragraphs.  Any element will work as long as it has this class and a unique ID -->
<p class="tab-text" id="home">HOME TEXT!!!</p>
<p class="tab-text" id ="about">ABOUT TEXT!!!</p>

        <h1>Little Shop </h1>

        <ul id="tabs">
            <li><a href="#home">Home</a></li>
            <li><a href="#about">About</a></li>
        </ul>

<script>
// Run when document is ready
(function() {
    // Find the UL parent of the tab links
    var tabs = document.getElementById('tabs');
    // Find all links inside it
    var tabLinks = tabs.getElementsByTagName('a');

    // Iterate through them and give them a click event handler
    for (var i = 0; i < tabLinks.length; i++)
    {
        tabLinks[i].onclick = function() {
            // Iterate over all the elements with a class of tab-text and hide them initially
            [].forEach.call(document.getElementsByClassName("tab-text"), function(el) {
              el.style.display = 'none';
            });

            // Then find the element whose ID matches the href attribute of the link clicked and show that
            document.getElementById(this.href.split('#')[1]).style.display = 'block';

            return false;
        }
    }
})();
</script>

然后,您可以使用它来查找表单的所有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