Jump to content
Xtreme .Net Talk

Recommended Posts

Posted
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?
  • *Experts*
Posted

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.

    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.

  • 1 month later...
  • *Experts*
Posted

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:

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:

AddHandler txt.Click, AddressOf TextBoxes_Click

 

And there you have it!

"Being grown up isn't half as fun as growing up

These are the best days of our lives"

-The Ataris, In This Diary

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...