CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Oct 1999
    Location
    Pune - Maharashtra - India
    Posts
    6

    Iterate through controls

    I have a VB 6.0 form containing a few labels and text boxes. I want to scroll through all these controls programatically and access the data inside (.text property). I also want the controls to be accessed in an order that I would specify at design time (say tab index order). I've tried the controls collection and the "for each item in collection" type loop ; but it gives me controls in an order for which I could not perceive any logic. Please let me know how to set this order. (Does anyone know what order the controls collection give by default ?)

    Thanks in advance



  2. #2
    Join Date
    Apr 1999
    Location
    Netherlands
    Posts
    181

    Re: Iterate through controls

    I think it's the order in which you placed the controls on the form at designtime (not the taborder).

    Crazy D @ Work :-)

  3. #3
    Join Date
    Oct 1999
    Location
    Pune - Maharashtra - India
    Posts
    6

    Re: Iterate through controls

    It is not even that order. To start with, it may appear so (Reverse order of your control insertion); but after adding a few more controls the order is unpredictable. It appears to group all text boxes together ,all labels together etc.
    But I could not find the exact logic.

    Thanks for the reply.


  4. #4
    Join Date
    Jul 1999
    Posts
    11

    Re: Iterate through controls

    The control list is a linked list with no useful order. If you only have a few, you can organize them by hand:

    dim oaControls(5) as control
    private Sub Form_Load()
    set oaControls(0)=Label1
    set oaControls(1)=Text1
    set oaControls(2)=Label2
    set oaControls(3)=Text2
    set oaControls(4)=Label3
    End Sub
    ...sometime later...
    ' text for Textbox and Label must be accessed with diff properties
    for i=0 to 4
    if typeof oaControls(i) is Label then
    whatIwant = oaControls(i).Caption
    else
    whatIwant = oaControls(i).Text
    end if
    next




    You might consider making the labels into Textboxes with input disabled, to make coding access easier. You can create them as a control array, such as Text1(0) through Text1(n). These are very easy to work with by indexing.



  5. #5
    Join Date
    Oct 1999
    Posts
    25

    Re: Iterate through controls

    If you want to iterate through controls in accordance to their tab order it can easily be done as follows:

    Sub GetControl (m_TabOrderNum as integer)
    Dim objControl As Control
    For Each objControl In Form1
    If objControl.TabIndex = m_TabOrderNum Then
    'do whatever, for example
    objControl.SetFocus ' note that you cannot set focus to a label control
    Exit For
    End If
    Next
    End Sub

    Sky1000


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

Featured