VB6 textbox array to what in .NET

robbremington

Newcomer
Joined
Jan 6, 2003
Messages
17
The app builds a flow chart of textBoxes and lines from values stored in a database. In VB6 textBoxes have an Index which allows them to be an array. How should I convert this to .NET, a collection, instantiating a textbox class?
 
You could create the TextBoxes in code (setting their properties and
such), and then add them to a Collection (or an ArrayList). For example,
this code creates 7 textboxes, adds them to a form (and an ArrayList),
and then modifies one of them using the ArrayList.
Visual Basic:
    Dim textBoxArray As New ArrayList()
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim i As Integer

        For i = 0 To 6
            Dim txt As New TextBox()

            With txt
                .Size = New Drawing.Size(95, 10)
                .Location = New Drawing.Point(5, 10 + (i * 20))
                .Text = "Text Box #" & i + 1
            End With

            Me.Controls.Add(txt)
            textBoxArray.Add(txt)
        Next

        DirectCast(textBoxArray(4), TextBox).Text = "Blah"
    End Sub
You need to use DirectCast if you want to use Option Strict, or if you want the intellisense list. Another option is
to create a class which inherits 'CollectionBase' and override the
Item property and Add method, so that you actually add
TextBoxes to the collection, rather than just Objects. These are
called typed collections.
 
How to know which text box was selected

I set the tag property of each text box as it's created.
When a user clicks one of text boxes WHAT CODE do I write to retrieve that tag value?:eek:
 
When you create each TextBox, use the AddHandler statement to
add a handler for that box's Click event to some sub you have
created that has the same signature as the click event. Then,
when that sub is called, the sender object will contain the TextBox
that was clicked, and you can cast this into a TextBox and retrieve
the Tag value.

This is the subroutine to handle the click events:
Visual Basic:
Private Sub TextBoxes_Click(sender As Object, e As EventArgs)
  Dim txt As TextBox

  txt = DirectCast(sender, TextBox)

  ' Then do whatever with txt.Tag
End Sub

Then, to make the sub an event handler, add the following code
inside the For loop of the code in Volte's post above:
Visual Basic:
AddHandler txt.Click, AddressOf TextBoxes_Click

And there you have it!
 
Back
Top