Results 1 to 8 of 8

Thread: [RESOLVED] [2008] Control Arrays in VB 2008

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Sep 2003
    Location
    Deer Park, NY
    Posts
    113

    Resolved [RESOLVED] [2008] Control Arrays in VB 2008

    Quick question,

    How do I do create control arrays in VB2008. In vb6.0 I was able to do

    vb.net Code:
    1. For i As Integer, 0 to 9, step 1
    2.        txtMSG(i).text = ("I am text box number: " + chr(i + 49))
    3. Next

    I need to be able to enumerate through a series of controls in this way.

    Thanks

    Brad Swindell
    IT Manager
    Electronix Systems C.S.A. Inc.

  2. #2
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: [2008] Control Arrays in VB 2008

    There is, but you have to change the way you think about it. There is no longer a way to create Control Arrays at design time... it has to be done at runtime... and it becomes and array of controls - jsut like any other array. If it's one type, like a button, you can use generics and make it a List (Of CommandButton). - or what ever else you need. And then in the constructor or the load, you have to figure out some way to load them into the array, list, what ever collection you use.

    Something similar to this should work:
    Code:
        Private Shared _dataCols() As DataColumn = {New DataColumn("TrackCode", GetType(String)), New DataColumn("RaceID", GetType(Integer)), New DataColumn("RaceNumber", GetType(String)), New DataColumn("PostTime", GetType(Date))}
    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: [2008] Control Arrays in VB 2008

    As tg says, arrays of controls are just like any other arrays in VB.NET.
    vb.net Code:
    1. Private textBoxes As TextBox()
    2.  
    3. Private Sub Form1_Load(ByVal sender As Object, _
    4.                        ByVal e As EventArgs) Handles MyBase.Load
    5.     Me.textBoxes = New TextBox() {Me.TextBox1, Me.TextBox2, Me.TextBox3}
    6. End Sub
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4
    PowerPoster BruceG's Avatar
    Join Date
    May 2000
    Location
    New Jersey (USA)
    Posts
    2,657

    Re: [2008] Control Arrays in VB 2008

    This article (series of articles) may be of interest:
    http://visualbasic.about.com/b/2008/...sual-basic.htm
    "It's cold gin time again ..."

    Check out my website here.

  5. #5
    Addicted Member
    Join Date
    Oct 2007
    Posts
    133

    Re: [2008] Control Arrays in VB 2008

    Here is a little example of a way you can do arrays in .net

    Add 3 text boxes and execute this code under a button.

    Code:
            Dim whatever() As TextBox = {TextBox1, TextBox2, TextBox3}
            Dim i As Integer = 0
            '
            For i = 0 To 2
                whatever(i).Text = "simple " & i
            Next
    VS 2015 Pro
    Windows 10 x64 Pro

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Sep 2003
    Location
    Deer Park, NY
    Posts
    113

    Re: [2008] Control Arrays in VB 2008

    Thanks guys that works perfectly. Will this also work with timers?

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: [2008] Control Arrays in VB 2008

    Quote Originally Posted by BKSwindell
    Thanks guys that works perfectly. Will this also work with timers?
    As we have stated fairly clearly already, an array is an array, whatever it contains. All arrays work the same way.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Sep 2003
    Location
    Deer Park, NY
    Posts
    113

    Re: [2008] Control Arrays in VB 2008

    Thanks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width