Solved

Loop through Controls in .NET

Posted on 2003-10-28
5
191 Views
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

Wayne
0
Comment
Question by:wlawson
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 3

Expert Comment

by:bacon7181
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()
        LoopThroughAllControls(Me.Controls)
    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
            LoopThroughAllControls(c.Controls)
        Next
    End Sub
0
 

Author Comment

by:wlawson
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.

Wayne
0
 

Author Comment

by:wlawson
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"
0
 
LVL 3

Accepted Solution

by:
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
            Return
        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
                LoopThroughAllControls(c)
            End If
        Next
    End Sub
0
 

Author Comment

by:wlawson
ID: 9646597
Top Shelf ... Worked a treat
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Run code from text file in vb 1 103
Export PDF Form fields to Access  or Excel  in Tab order 16 129
Powerpoint 2013: Change cell reference in excel link 3 140
VB6 Enviroment 3 37
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

752 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