Results 1 to 16 of 16

Thread: [RESOLVED] [2005] Order of Iteration through controls

Threaded View

  1. #1

    Thread Starter
    Hyperactive Member ZaNi's Avatar
    Join Date
    Jun 2006
    Location
    Australia
    Posts
    360

    Resolved [RESOLVED] [2005] Order of Iteration through controls

    I have been working on a project which needs to accept input from a number of textboxes. In order to minimize redundant code (and because I believe it is good practice), I have written one function that iterates through all the controls, Identifies them by their name, then adds them to the relevant list(there are three lists of textboxes, one list of comboboxes, and one list of labels). This function runs once on startup and sets some global arrays up with all the references they need for the rest of the program.

    So far so good, but upon execution, the order is wrong: my first two arrays of textboxes are in the correct order, but the third is in reverse! I have made a small attempt to write a function to reverse the third list (which is not working as of this point), but it seems silly to write a function because the controls are being iterated through in the wrong order...

    This leads to my question: Is there a way to alter the order in which controls as iterated through in this loop?

    VB Code:
    1. for each ctl as control in me.controls
    2.   'Do stuff with control
    3. next ctl

    Any information about this would be appreciated. I have included some relevant segments of code below.

    VB Code:
    1. ' These strings are used to identify which type of control I'm looking at
    2.     Const boxNoOfBridgesIDString As String = "NoOfBridges"
    3.     Const firstBridgingIDString As String = "txtBridginga"
    4.     Const secondBridgingIDString As String = "txtBridgingb"
    5.     Const thirdBridgingIDString As String = "txtBridgingc"
    6.     Const labelNoIDString As String = "Label"
    7.  
    8.     ' Global declaration so they are accessable when I need them
    9.     Dim boxNoOfBridges() As ComboBox
    10.     Dim txtFirstBridge() As TextBox
    11.     Dim txtSecondBridge() As TextBox
    12.     Dim txtThirdBridge() As TextBox
    13.     Dim labelNo() As Label
    14.  
    15.     Private Sub setupBridging()
    16.         Dim boxcount = 0
    17.         Dim txtfirstcount = 0
    18.         Dim txtsecondcount = 0
    19.         Dim txtthirdcount = 0
    20.         Dim labelcount = 0
    21.         For Each ctl As Control In TableBridging.Controls
    22.             If (ctl.Name.Length > boxNoOfBridgesIDString.Length) Then
    23.                 If (ctl.Name.Substring(0, boxNoOfBridgesIDString.Length) = boxNoOfBridgesIDString) Then
    24.                     ReDim Preserve boxNoOfBridges(boxcount)
    25.                     boxNoOfBridges(boxcount) = ctl
    26.                     boxcount += 1
    27.                 End If
    28.             End If
    29.             If (ctl.Name.Length > firstBridgingIDString.Length) Then
    30.                 If (ctl.Name.Substring(0, firstBridgingIDString.Length) = firstBridgingIDString) Then
    31.                     ReDim Preserve txtFirstBridge(txtfirstcount)
    32.                     txtFirstBridge(txtfirstcount) = ctl
    33.                     txtfirstcount += 1
    34.                     'MsgBox("Init: " + ctl.Name)
    35.                 End If
    36.             End If
    37.             If (ctl.Name.Length > secondBridgingIDString.Length) Then
    38.                 If (ctl.Name.Substring(0, secondBridgingIDString.Length) = secondBridgingIDString) Then
    39.                     ReDim Preserve txtSecondBridge(txtsecondcount)
    40.                     txtSecondBridge(txtsecondcount) = ctl
    41.                     txtsecondcount += 1
    42.                 End If
    43.             End If
    44.             If (ctl.Name.Length > thirdBridgingIDString.Length) Then
    45.                 If (ctl.Name.Substring(0, thirdBridgingIDString.Length) = thirdBridgingIDString) Then
    46.                     ReDim Preserve txtThirdBridge(txtthirdcount)
    47.                     txtThirdBridge(txtthirdcount) = ctl
    48.                     txtthirdcount += 1
    49.                 End If
    50.             End If
    51.             If (ctl.Name.Length > labelNoIDString.Length) Then
    52.                 If (ctl.Name.Substring(0, labelNoIDString.Length) = labelNoIDString) Then
    53.                     ReDim Preserve labelNo(labelcount)
    54.                     labelNo(labelcount) = ctl
    55.                     labelcount += 1
    56.                 End If
    57.             End If
    58.         Next
    59.  
    60.     ' At this point, for some reason, txtThirdBridge() is in reverse
    61.  
    62.     End Sub
    Last edited by ZaNi; Jun 13th, 2006 at 11:05 PM. Reason: Slight error in copied code

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