none
Text Box Arrays

    Question

  • Visual Basic 6 had the Text Box Array that worked great. It no longer exists  (in the same form) in either Visual Basic 20085 or 2010.  I know that there are work-arounds that emulate the Text Box array but these are certainly not as easy to use as the version 6 Text Box Array.

    In Visual Basic 2010 there is a tool box item that can be added, called Text Box Array. However, this does not place a Text Box on the form and I can't find any explanation of how to use it.

    Can anyone tell me what adding this to a form accomplishes and how to use it?

     


    sirmilt
    Wednesday, September 28, 2011 2:50 PM

Answers

  • sirMIlt,

    The way the the textbox array like it was as an automatically feature for creating it in the designer of the windows forms,  does not exist anymore. But it is improved

    You can make it simply by.

    Private myTextBoxArray as TextBox()

    And then in the method which handles the form load event

    myTextBoxArray = {Textbox1, Textbox2, Texbox3}

    But currently you can use it also for an array of all kind of controls which was impossible with VB6

     

     


    Success
    Cor


    Wednesday, September 28, 2011 3:16 PM
  • You need to indicate that you want an array by using parentheses:

    Private txtTrackArray() As TextBox

    If you put Option Strict On at the very top of the script, it might point out errors like that for you.

    HTH,

    Andrew

    Sunday, October 09, 2011 5:26 PM
  • Arrays in VB.Net start at index number 0 and not 1. So to process all 15 of them you need

    For i = 0 To 14
    


    or

    For i = 1 To txtTrackArray.Length - 1
    


    • Marked as answer by sirMilt Saturday, December 24, 2011 1:35 PM
    Saturday, December 24, 2011 3:33 AM
  • For i = 1 To txtTrackArray.Length - 1

    I think you meant to type For i = 0 To txtTrackArray.Length - 1

    --
    Andrew

    • Marked as answer by sirMilt Saturday, December 24, 2011 1:35 PM
    Saturday, December 24, 2011 11:26 AM
  • For i = 1 To txtTrackArray.Length - 1

    I think you meant to type For i = 0 To txtTrackArray.Length - 1

    --
    Andrew

    Oops. You're right of course. Thanks for pointing that out.
    • Marked as answer by sirMilt Saturday, December 24, 2011 1:36 PM
    Saturday, December 24, 2011 12:57 PM

All replies

  • sirMIlt,

    The way the the textbox array like it was as an automatically feature for creating it in the designer of the windows forms,  does not exist anymore. But it is improved

    You can make it simply by.

    Private myTextBoxArray as TextBox()

    And then in the method which handles the form load event

    myTextBoxArray = {Textbox1, Textbox2, Texbox3}

    But currently you can use it also for an array of all kind of controls which was impossible with VB6

     

     


    Success
    Cor


    Wednesday, September 28, 2011 3:16 PM
  • One thing I have done is that I gave them a 'same' tag in the properties.

     

    Then looped through the controls on the form and built my array that way.

     

     

    Wednesday, September 28, 2011 3:25 PM
  • Hello,

    hiere is a similar code from me. Uses Textboxes which are dynamically created at runtime into an array of textbox.

    Perhaps may be helpful.

    regards Ellen

    textboxes

     

    code:

    Public Class Form1
    
      Private tb(9, 9) As TextBox
    
      Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) _
    
      Handles MyBase.Load
    
    
    
        For i = 0 To 9
    
          For j = 0 To 9
    
            tb(i, j) = New TextBox
    
            tb(i, j).Name = String.Format("box{0}{1}", i.ToString, j.ToString)
    
            tb(i, j).Text = i.ToString & ">" & j.ToString
    
            tb(i, j).Size = New Size(40, 20)
    
            tb(i, j).Location = New Point((40 * i) + 1, 21 * j)
    
            tb(i, j).BorderStyle = BorderStyle.Fixed3D
    
            Me.Controls.Add(tb(i, j))
    
          Next j
    
        Next i
    
      End Sub
    
    End Class
    
    

     


    Ich benutze/ I'm using VB2008 & VB2010
    Wednesday, September 28, 2011 4:02 PM
  • Thanks to both of you for the quick answers. What confused me was the 'Text Box Array' that you can add from  the Tool box items.  Does it serve any purpose? Is it necessary or even desired in order to follow your simple, but excellent , suggestions?

    Milt


    sirmilt
    Wednesday, September 28, 2011 10:20 PM
  • Is this an upgraded project from VB6 to VB 2008 (then upgraded to VB 2010)? In this case their might still be the item in the Toolbox. The full name is Microsoft.VisualBasic.Compatibility.VB6.TextBoxArray and shouldn't be used in new projects anymore.
    Armin
    • Marked as answer by sirMilt Wednesday, September 28, 2011 11:26 PM
    • Unmarked as answer by sirMilt Sunday, October 09, 2011 4:01 PM
    Wednesday, September 28, 2011 10:50 PM
  • Thank you all for the help. I just got around to trying to insert the textbox array into my project and have a problem.

    This code:

       Private txtTrackArray As TextBox

        Private Sub frmCollectionsCDs_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

            txtTrackArray = {txtTrack1, txtTrack2, txtTrack3, txtTrack4, txtTrack5, txtTrack6, txtTrack7, txtTrack8, txtTrack9, txtTrack10, txtTrack11, txtTrack12, txtTrack13, txtTrack14, txtTrack15}

    Produces the error:

    Value of type '1-dimensional array of System.Windows.Forms.TextBox' cannot be converted to 'System.Windows.Forms.TextBox'.

    This code:

    Private txtTrackArray As TextBox

        Private Sub frmCollectionsCDs_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

            txtTrackArray = (txtTrack1, txtTrack2, txtTrack3, txtTrack4, txtTrack5, txtTrack6, txtTrack7, txtTrack8, txtTrack9, txtTrack10, txtTrack11, txtTrack12, txtTrack13, txtTrack14, txtTrack15)

    Produces the error:

      ')' expected.

    If I add the second close paren at the end. the error remains but the pointer is at the comma after txtTrack1, which would seam to indicate something, but I can't figure out what.

    Milt


    sirmilt
    Sunday, October 09, 2011 4:43 PM
  • You need to indicate that you want an array by using parentheses:

    Private txtTrackArray() As TextBox

    If you put Option Strict On at the very top of the script, it might point out errors like that for you.

    HTH,

    Andrew

    Sunday, October 09, 2011 5:26 PM
  • Hi again

    This subject really has me baffled. Cor's suggested code seemed to do the trick...but throws an error when running. Here's my code:

    Under the class I added this code:

     Private txtTrackArray() As TextBox

    Since i plan to use this array several times, I added the following to the form load:

     txtTrackArray = {txtTrack1, txtTrack2, txtTrack3, txtTrack4, txtTrack5, txtTrack6, txtTrack7, txtTrack8, txtTrack9, txtTrack10, txtTrack11, txtTrack12, txtTrack13, txtTrack14, txtTrack15}

    I wanted to check and make sure that at least one of the items in the array had an entry, or all were empty so I added this code"
                Dim i As Integer = 0
                Dim n As Integer = 0
                For i = 1 To 15
                    If txtTrackArray(i).Text <> Nothing Then n = n + 1
                Next
    I followed the execution and this error at 15:
    Index was outside the bounds of the array.
    Milt


    sirmilt
    • Marked as answer by sirMilt Saturday, December 24, 2011 1:35 PM
    • Unmarked as answer by sirMilt Saturday, December 24, 2011 1:35 PM
    Saturday, December 24, 2011 1:32 AM
  • Arrays in VB.Net start at index number 0 and not 1. So to process all 15 of them you need

    For i = 0 To 14
    


    or

    For i = 1 To txtTrackArray.Length - 1
    


    • Marked as answer by sirMilt Saturday, December 24, 2011 1:35 PM
    Saturday, December 24, 2011 3:33 AM
  • For i = 1 To txtTrackArray.Length - 1

    I think you meant to type For i = 0 To txtTrackArray.Length - 1

    --
    Andrew

    • Marked as answer by sirMilt Saturday, December 24, 2011 1:35 PM
    Saturday, December 24, 2011 11:26 AM
  • For i = 1 To txtTrackArray.Length - 1

    I think you meant to type For i = 0 To txtTrackArray.Length - 1

    --
    Andrew

    Oops. You're right of course. Thanks for pointing that out.
    • Marked as answer by sirMilt Saturday, December 24, 2011 1:36 PM
    Saturday, December 24, 2011 12:57 PM
  • Thank you all for the help,  the fact that arrays start with 0 completely slipped my mind, until  about 3AM last night.

    Using the track length to test for an empty text box is something I hadn't thought of. it too works fine.

    Milt

     

     


    sirmilt
    Saturday, December 24, 2011 1:35 PM