Click to See Complete Forum and Search --> : Iterate through controls
Anand Patwardhan
October 19th, 1999, 08:19 AM
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
Crazy D @ Work
October 19th, 1999, 08:53 AM
I think it's the order in which you placed the controls on the form at designtime (not the taborder).
Crazy D @ Work :-)
Anand Patwardhan
October 19th, 1999, 09:37 AM
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.
Harry Gilbert
October 19th, 1999, 09:42 AM
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.
Sky1000
October 19th, 1999, 05:58 PM
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
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.