Solved

control arrays in vb 2005

Posted on 2006-06-27
5
225 Views
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
    Next
End Sub

thanks

Max
0
Comment
Question by:maxgall
  • 2
  • 2
5 Comments
 
LVL 48

Accepted Solution

by:
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
    Next
End Sub
0
 
LVL 19

Expert Comment

by:arif_eqbal
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

    Controls.Item(str)

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

0
 

Author Comment

by:maxgall
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 = date.now()
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.



0
 
LVL 48

Expert Comment

by:AlexFM
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.
0
 

Author Comment

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

thanks vm
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

911 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now