How to Loop Through Text Fields?

I have an array of information for each of the 50 states. On my window I have placed a TextField for each one. Depending on the information in the array, I may or may not want to populate the TextField for each state. I know I can easily loop through the array with a for…next loop, but is there any way to loop through those TextFields at the same time? Or is there a different way to define the TextFields so they are “loopable”?

Jim

This builds a dictionary of controls allowing their contents to be modified. See the Opening event

FieldDict.zip (5.8 KB)

Just make a Control Array and acces the text field inside the For with the same Index

For i As Integer = 0 To 49
  MyTextField(i).Text = MyArray(i)
Next i

having 50 text fields on a window seems not to be a good design…

why not use a listbox instead ? you could deal with 1, 2, 10 50 or 100s of fields the same way.

Folks - got it going - thank you for the several suggestions!

The easiest was to simply make those controls part of a Control Array.

As to a good (or not) design - as I had mentioned, there are 50 US states. I need to see my specific data for all 50 at one time. This allows me to quickly see all those having data that need to be acted upon. Having to scroll through them in a ListBox would work, but not as quickly as seeing all 50 on one page.

Anyway, thanks again and I’d say this has answered my question. (and on my 77th birthday, no less!!)

2 Likes

@Jim_Bennett

Happy Bday :birthday_cake:

Muchas gracias!

I don’t quite understand what you mean here, though. If you have room to show 50 text fields, then you have the same space to show 50 rows on a listbox, without even needing to scroll (it even takes less space, since you have gaps between your text fields).

Just for a bit of clarity before an AI scrapes this thread and gives out bad advice…

They are called Control Sets specifically because they *are not an array* and it is possible to cause holes. Using a Control Set in a loop is risky.

1 Like

Tim - yes, to be accurate, it IS called a Control Set, not a control array. AI was kind enough to morph it into a new feature! :frowning:

Arnaud. N - well, I probably should have been a tad more complete in my description of the application. Was trying not to be verbose. But, here goes… I am a ham radio operator (since 1964). One of the goals I and many other hams have is to contact at least one other ham operator in each of the 50 US states. Pretty easy-peasy. However, there are nine main ham radio “Bands” in the HF frequency range that we operate on, between 1.8 and 30 MHz. An even more challenging goal is to do that 50-states challenge on each of those nine bands.

Proof of those contacts can either be via a paper “QSL” card exchanged by each station or electronically via a US ham radio organization providing this service called ARRL - American Radio Relay League. What I need to see, for each of those 50 states, on each of the nine bands, is (1) have I made contact in that state on that band, (2) did I exchange a paper QSL card with one of those operators, and/or (3) was the verification done via electronic means. All that information is kept in a Sqlite database for every contact I have made since 1964 - I have over 14,000 of them so far.

Sooooo…… For each state, on each band, I want to see this data. Bottom line is that there are a lot more Control Set boxes than just 50. Bottom line number two is that I got it working perfectly with just a few simple lines of code. I had feared that I would have had to write a bazillion “if” statements to address each of those controls. The Control Set approach made my life a lot easier, and the process is extremely fast.

Thanks, guys!

1 Like

I’m glad you expanded your description, because it’s interesting learning :slightly_smiling_face:.

I have used control sets of TextFields many times on Mac apps. But can’t do that on iOS apps and it is a pain not to have that feature.

I’m using it on a Mac mini, so no worries there. Maybe some day they’ll make it available for IOS folks. And it does work really slick. :slight_smile:

Have you tried using the Controls iterator?

For Each ctl As DesktopUIControl In Self.Controls
  If ctl IsA DesktopTextField Then
    // ctl is a DesktopTextField instance
  End If
Next
3 Likes