Results 1 to 25 of 25

Thread: Control Arrays

Threaded View

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949

    Control Arrays

    Hi,

    A common misconception is that Control Arrays have disappeared from VB.NET. They haven't. What has disappeared is the Index Property of a control and the ability to create the array automatically in design view.

    MSDN Help will give several work-arounds, including use of the Tag property but the simplest way is to use an Array of Controls. The following creates ten textboxes in code and stores a reference to each one in an array.

    VB Code:
    1. Dim arrTextBox(0) As TextBox  ' (Prepare array to receive textBoxes.
    2.                                                  '  Give this the necesary scope)
    3.  
    4.     Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
    5.  
    6.         Dim txtTemp As TextBox       '   ( Reserve variable for new textbox
    7.                                                 '   Local scope only  OK)
    8.         Dim iCount As Integer              (Local scope only)
    9.  
    10.         For iCount = 0 To 9
    11.             ReDim Preserve arrTextBox(iCount)  '(Expand array to receive
    12.                                                                  ' next textbox
    13.             txtTemp = New TextBox   ' ( Create instance of new textbox)
    14.             txtTemp.Name = "txtBox" & iCount.ToString  '(Name textboxes with
    15.                                                                         'sequential numbering)
    16.             txtTemp.Width = 40
    17.             txtTemp.Location = New Point(15, iCount * 20)
    18.             Me.Controls.Add(txtTemp)           '(Add new textbox to form
    19.             arrTextBox(iCount) = txtTemp      '(Place reference to new textbox
    20.                                                           'in appropriate element of array)
    21.  
    22. '(The next line adds the handle of the new textbox to the appropriate event)
    23.  
    24.             AddHandler txtTemp.TextChanged, AddressOf TextChangedEvent 
    25.         Next
    26. End Sub
    27.  
    28.  
    29.      Private Sub TextChangedEvent(ByVal sender As System.Object, ByVal e As System.EventArgs)
    30.         MessageBox.Show(CType(sender, TextBox).Name & "  Text was changed")
    31.     End Sub

    You can now refer to each TextBox by the array subscript ( index no.)

    This is better than the VB6 Control Array because you can mix any type of object by replacing

    Dim txtTemp As TextBox
    Dim arrTextBox() As TextBox
    txtTemp = New TextBox

    with

    Dim txtTemp As Object
    Dim arrTextBox() As Object
    txtTemp = New Object

    You could then widen the possibilities by using a 2 dimension array, allocating the elements of the first dimension to hold different types or groups of objects, although those elements would only hold numeric values of course.


    EDIT; Handler for Text_Changed event added, see Wossname's comment.
    Last edited by taxes; Mar 11th, 2005 at 06:02 AM.
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width