control arrays in vb 2005

Posted on 2006-06-27
Last Modified: 2010-04-23
In VB 6 or earlier I have used control arrays before. I understand they are not supported anymore in vb 2005.
I have tried to look the workings of it up but still have a problem accessing an individual control by index.

I have 6 textboxes withing a tablelayout. In VB 6 I would have created a control array and accessed the contain like below:

Dim i as integer = 4   ' number comes from some other code
textbox(i).text = "some Text"

How can I do this in vb 2005.

The code below lets me change all of my textboxes but I am struggling if I want to change the text for only one of the textboxes like above.

Private Sub ClearText(ByVal container As Control)
    Dim ctrl As Control
    For Each ctrl In container.Controls
        If TypeOf (ctrl) Is TextBox Then
            ctrl.Text = "some text"
        End If
End Sub


Question by:maxgall
  • 2
  • 2
LVL 48

Accepted Solution

AlexFM earned 250 total points
ID: 16990669
Private Sub SetText(ByVal textBoxNumber as Integer, ByVal text as String)
    Dim ctrl As Control
    Dim n As Integer = 0

    For Each ctrl In container.Controls
        If TypeOf (ctrl) Is TextBox Then
            if ( n = textBoxNumber ) Then
                ctrl.Text = text
                Exit Sub
            End If
            n = n + 1
        End If
End Sub
LVL 19

Expert Comment

ID: 16990753
Hi maxgall
VB6 gave a good visual way of creating control array, and we used to access it using the index because they all had the same Name
However in .NET we do not have more than one control with the same name, so earlier we neede to know the index to access the control now we need to know the Name
So if you know the name of the control instead of
     Dim i as integer = 4   ' number comes from some other code
you would have
     Dim str as String= "Txt1"   ' Name comes from some other code
Then you can access it like this


so you can have     Controls.Item(str).Text = "Some New value"


Author Comment

ID: 16990980
Ok thanks vm bit confused now not sure if I can use string. The easiest is probably to show you what I need. see below:

Dim isToday =
Dim intDayofWeek as integer = istoday.dayofWeek     ' this returns integer

textbox(intDayofWeek).text = "some text"

I think it was as easy as that before. What would you recommend I do for this example please.

LVL 48

Expert Comment

ID: 16990991
You can use function from my first post:

SetText(intDayofWeek - 1, "some text")

SetText uses 0-based indexing, you can change this if you want.

Author Comment

ID: 16991144
I have allocated the points to AlexFM as that solution works fine for me and was the first.

thanks vm

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VB.NET 1 28
get column names from table in 8 49 and sql server 4 45
Winform Module - What is the ASP.Net equiv 2 28
Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below.…

808 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