indexing text fields in 2003

Posted on 2004-10-27
Last Modified: 2010-08-05
i am a vb6 programmer trying to learn
i have several vb6 programs that i have converted to
i like to use indexed text fields.  the .net conversion converted those fields ok.
i don't understand how to add another indexed text field to an existing group of indexed text fields in .net

any help


Question by:BHoward
    1 Comment
    LVL 85

    Accepted Solution

    The standard VB.Net answer is:  You don't...VB.Net doesn't support control arrays...use VB.Net event handling instead.

    It is possible, however, even if it is not the recommended way of doing things.

    When you convert a control array of textboxes to VB.Net, each indexed textbox in the array is changed into an individual textbox.  Then, an object of type Microsoft.VisualBasic.Compatibility.VB6.TextBoxArray is created to handle the events for the array.  Each of the individual textboxes is then registered with the TextBoxArray and given an index value.  Finally, the EndInit() method for the TextBoxArray is called.  All of this is done inside the "Windows Form Designer generated code" region (near the top of the form with the + symbol) that you are ~not supposed to touch~.

    So in VB6, if you had a TextBox control array called Text1 with three controls in it (indices 0, 1, and 2), they would be converted to three TextBoxes called _Text1_0, _Text1_1, and _Text1_2, with a supporting TextBoxArray object called Text1.  The code below shows how the three individual textboxes are registered as part of the control array with a specific index (pulled from different parts of the Form Designer region):

        Public WithEvents Text1 As Microsoft.VisualBasic.Compatibility.VB6.TextBoxArray
        Me.Text1 = New Microsoft.VisualBasic.Compatibility.VB6.TextBoxArray(Me.components)
        Me.Text1.SetIndex(Me._Text1_0, CType(0, Short))
        Me.Text1.SetIndex(Me._Text1_1, CType(1, Short))
        Me.Text1.SetIndex(Me._Text1_2, CType(2, Short))
        CType(Me.Text1, System.ComponentModel.ISupportInitialize).EndInit()

    So if you wanted to add another TextBox to the array, you would simply need to register it with the SetIndex() function BEFORE the call the EndInit() method.  This means you must add this in the Form Designer region somewhere.  Lets say we added a TextBox to the form called TextBox1.  To register it with the control array as index 3, go into the Form Designer region and find the area for TextBox1.  It may look something like this:

            Me.TextBox1.Location = New System.Drawing.Point(8, 112)
            Me.TextBox1.Name = "TextBox1"
            Me.TextBox1.Size = New System.Drawing.Size(152, 20)
            Me.TextBox1.TabIndex = 4
            Me.TextBox1.Text = "TextBox1"

    Then, you would add this line to that block of code:

            Me.Text1.SetIndex(Me.TextBox1, CType(3, Short))

    That's it.  Now TextBox1 is part of the control as index 3.

    Of course, everyone is going to yell at me for not teaching you the VB.Net way so I better do that as well.  The TextBoxArray object traps the events for the individual textboxes and then raises them again using its built-in events.  You determine the index of the sender by using its GetIndex() function as shown below:

        Private Sub Text1_TextChanged(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles Text1.TextChanged
            Dim Index As Short = Text1.GetIndex(eventSender)
            Label1.Text = Text1(Index).Text
        End Sub

    This same functionality can be achieved by adding more than event after the Handles keyword at the end of the first line.  So to wire up a sub that handles all of the TextChanged events for the four textboxes (remember we added a textbox) you would do something like this:

        Private Sub Common_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) _
                Handles _Text1_0.TextChanged, _Text1_1.TextChanged, _Text1_2.TextChanged, TextBox1.TextChanged
            Label3.Text = sender.Text
        End Sub



    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Prepare to Pass the CompTIA A+ 900 Series Exam

    CompTIA aims to adapt its A+ Certification to reflect the most current knowledge and skills needed by today's IT professionals--and this year's 2016 exam is harder than ever. This certification is one of the most highly-respected and sought after in IT.

    Suggested Solutions

    Title # Comments Views Activity
    dog bark java program 15 61
    lucky13 challenge 11 83
    fizzArray3  challenge 1 40
    thread-safe code in c++ 2 40
    INTRODUCTION We all know how to code. But at times you simply want to insert a common code block into your existing code and amend it as per your requirements. This tool not only saves you time but also saves you the pain of typing it all out aga…
    This is about my first experience with programming Arduino.
    In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
    In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

    846 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

    8 Experts available now in Live!

    Get 1:1 Help Now