Results 1 to 1 of 1

Thread: Visit Every Control on a Form (includes nested controls, no recursion)

Threaded View

  1. #1

    Thread Starter
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Visit Every Control on a Form (includes nested controls, no recursion)

    VB version here.

    This code will visit every control on a form, regardless of how deeply it's nested. You could have a TextBox in a Panel in a Splitter in a GroupBox in a TabPage in a TabControl in a Panel and it will still be found. No recursion is need on your part, as this will simply follow the tab order from start to finish. It also doesn't matter if a control has it's TabStop set to False, as it must still have a TabIndex.
    Code:
    Control ctl = this.GetNextControl(this, true); // Get the first control in the tab order.
    
    while (ctl != null)
    {
        // Use ctl here.
    
        ctl = this.GetNextControl(ctl, true); // Get the next control in the tab order.
    }
    No doubt the GetNextControl method uses recursion internally, but that's not our concern. This will keep your code nice and neat and you'd need a very, very large number of controls before you'd notice a difference in performance.
    Last edited by jmcilhinney; Oct 29th, 2008 at 08:07 PM.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width