Avatar of koossa
koossa

asked on 

Quicker way to loop through certain controls

I have a lot of controls on my form and want to loop through eg the text boxes.

I'm currently using this
For Each Control In Controls

Is there a quicker way to get a collection of only the text boxes?
Visual Basic Classic

Avatar of undefined
Last Comment
koossa
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

VB6 or VB.Net?

If VB6 then not really.
If VB.Net then you can use LINQ to grab just the type you are interested in.

For both, you can use a Collection (VB6) or a List(VB.Net) and iterate just ONCE at Form Load so you can add the controls to that Collection/List.  Then future operations can simply iterate the stored references instead of iterating over all controls.
Avatar of koossa
koossa

ASKER

Hi, it is VB6

Can you give me a quick example of how to add the controls to a collection.
Is it at design time?
It would happen at run-time...something like:
Dim TBs As New Collection

Private Sub Form_Load()
    Dim ctl As Control
    For Each ctl In Me.Controls
        If TypeOf ctl Is TextBox Then
            TBs.Add ctl, ctl
        End If
    Next
End Sub

' An Example of How to Iterate over the TextBoxes:
Private Sub Foo()
    Dim TB As Control
    For Each TB In TBs
        ' ... do something with "TB" ...
    Next TB
End Sub

Open in new window

Avatar of koossa
koossa

ASKER

Ok, but my "For Each Control In Controls" is currently in my form_load and I want my form_load to be faster, is there any way to do that?
Not really.  You could side step the issue by using a Timer to delay the iteration of the Controls collection to after the form has been completely shown.

You could try giving the illusion that the form is loading faster by forcing the form to show itself before continuing on with other code:
*this doesn't really make it any faster!*

    Private Sub Form_Load()
        Me.Show
        DoEvents

        ' ... continue with other code ...
    End Sub
Avatar of koossa
koossa

ASKER

Can't I create a collection for example of textboxes in design time?
Not that I'm aware of...not unless you make them all part of the same container like a GroupBox...and they would need to be the only thing in that container.
Avatar of koossa
koossa

ASKER

Ok, if I put them all in the same container, how would I then be able to loop through them?
ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of koossa
koossa

ASKER

Thank you
Visual Basic Classic
Visual Basic Classic

Visual Basic is Microsoft’s event-driven programming language and integrated development environment (IDE) for its Component Object Model (COM) programming model. It is relatively easy to learn and use because of its graphical development features and BASIC heritage. It has been replaced with VB.NET, and is very similar to VBA (Visual Basic for Applications), the programming language for the Microsoft Office product line.

165K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo