Loop through Controls in .NET

Posted on 2003-10-28
Last Modified: 2010-05-01
You would think this would be the easiest task to do in VB .Net, but the old way of doinf thinsg does not seem to work
And do you reckon I can find it anywhere on the Microsoft Site?

Can someone tell me how I loop through all Textboxes on a Form to simply empty them.
The textboxes are inside a TabPage

Question by:wlawson
  • 3
  • 2

Expert Comment

ID: 9639216
I just recently had to loop through all the controls on a form for something.

This is just one way that you could implement this loop, this uses a recursive function.

    Public Sub DoTheLoop()
    End Sub

    Private Sub LoopThroughAllControls(ByVal controls As ControlCollection)
        Dim c As Control
        For Each c In controls
            If GetType(TextBoxBase).IsInstanceOfType(c) Then
                ' c is a textbox based control, do something with it
            End If
            ' loop through sub controls
    End Sub

Author Comment

ID: 9639282
Thanks for that it works well for all Controls on the actual form, but it will not work for anything within the TabPage Control.


Author Comment

ID: 9639341
I can see what your code is trying to do and if feed the TabControl into your Sub Rountine I can count the Controls on the page, but when I try to loop through the controls I get an error "Specific Cast is not valid"

Accepted Solution

bacon7181 earned 50 total points
ID: 9642965
Try this one:

    Private Sub LoopThroughAllControls(ByVal parentControl As Control)
        Dim c As Control

        ' Make sure there are children to iterate over
        If Not c.HasChildren Then
        End If

        For Each c In parentControl.Controls
            ' Find children of desired type
            If GetType(TextBox).IsInstanceOfType(c) Then
                ' c is a textbox based control, do something with it
                c.Text = "Howdy"
            End If
            ' If this control also has children
            If c.HasChildren Then
                ' loop through sub children
            End If
    End Sub

Author Comment

ID: 9646597
Top Shelf ... Worked a treat

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
vb6 programatically bring window to front 8 71
How to debug this code 7 61
passing parameter in sql procedure 9 61
Excel VBA, find a string in a column, update a cell 7 78
Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question