Results 1 to 40 of 47

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)

    C# 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 needed 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.
    VB Code:
    1. Dim ctl As Control = Me.GetNextControl(Me, True) 'Get the first control in the tab order.
    2.  
    3. Do Until ctl Is Nothing
    4.     'Use ctl here.
    5.  
    6.     ctl = Me.GetNextControl(ctl, True) 'Get the next control in the tab order.
    7. Loop
    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; Jul 24th, 2021 at 12:56 AM.
    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