robbremington Posted January 17, 2003 Posted January 17, 2003 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? Quote
*Experts* Volte Posted January 17, 2003 *Experts* Posted January 17, 2003 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 SubYou 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. Quote
robbremington Posted February 19, 2003 Author Posted February 19, 2003 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: Quote
*Experts* Bucky Posted February 19, 2003 *Experts* Posted February 19, 2003 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! Quote "Being grown up isn't half as fun as growing up These are the best days of our lives" -The Ataris, In This Diary
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.