Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


indexing text fields in vb.net 2003

Posted on 2004-10-27
Medium Priority
Last Modified: 2010-08-05
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


Question by:BHoward
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
LVL 86

Accepted Solution

Mike Tomlinson earned 1000 total points
ID: 12423824
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



Featured Post

Enroll in October's Free Course of the Month

Do you work with and analyze data? Enroll in October's Course of the Month for 7+ hours of SQL training, allowing you to quickly and efficiently store or retrieve data. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
What do responsible coders do? They don't take detrimental shortcuts. They do take reasonable security precautions, create important automation, implement sufficient logging, fix things they break, and care about users.
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…
Introduction to Processes

609 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