indexing text fields in vb.net 2003

i am a vb6 programmer trying to learn vb.net.
i have several vb6 programs that i have converted to vb.net.
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

bill

BHowardAsked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
Mike TomlinsonConnect With a Mentor Middle School Assistant TeacherCommented:
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:

        '
        'TextBox1
        '
        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

Regards,

Idle_Mind
0
All Courses

From novice to tech pro — start learning today.