PDA

View Full Version : Solved: Sequence of controls during For-Next loop



village_alchemist
09-05-2004, 07:10 PM
I have a series of CheckBox controls on a form. Some of them are dependent on the value of others. Is there a property (that I can modify) that determines the sequence that the controls are retrieved during this loop?

For Each ctlInForm In frmItems.Controls
Next ctlInForm

I was looking for an eloquent solution, but it's looking like I'll need to brute force it. :viking:

Jacob Hilderbrand
09-05-2004, 07:22 PM
Its the order they are added.


For Each ctlInForm In frmItems.Controls
MsgBox ctlInForm.Name
Next ctlInForm

Jacob Hilderbrand
09-05-2004, 07:26 PM
You can also loop through them like this:

For x = 1 To 10
MsgBox Me.Controls("CheckBox" & x).Value
Next x



Assuming their names are CheckBox1, CheckBox2 ...

mdmackillop
09-06-2004, 01:51 PM
Hi,
If your controls are not sequential, you could use an array of the selected order. This could be modified to suit named controls if required.
MD
CBs = Array("3", "1", "2")
for each c in CBs
MsgBox Me.Controls("CheckBox" & c).Value
next

fumei
09-06-2004, 10:20 PM
The original post regarded conditional logic on a control. if ONE is a particular value then make another one a particular value.

Why not just do it by explicit naming of the control? If controlX = blah, then controlY = blahblah2. Am I missing something, ....again?

Alternatively, you could set the TabIndexes of the checkboxes, and use that.


Dim myControl As Control
For Each myControl In frmTestTabOrder.Controls
If myControl.TabIndex = 2 Then ' a specific checkbox
If Me.Controls(myControl.Name).Value = True Then
' some other control = true
' or whatever
End If
End If
Next

village_alchemist
09-07-2004, 05:03 AM
Gerry, that's what I was planning to do, except, like DrJ stated, they are sequenced by the order they were added to the form which is not the order I need to get them in. Some of the functions are inter-dependent and if processed in the wrong order cause an incorrect result. I could delete and re-add them in the correct order, but I'd rather not rely on something that I can't control.

I'm gathering that, other than adding the controls in the correct order or putting them into an array before I process them, that there is no way to control the order that they are retrieved. Oh well, guess I'll re-write my code.

Thanks for your help! :hi:

Jacob Hilderbrand
09-07-2004, 02:12 PM
Just name them in the order you want, sequentially. Then use this:


For x = 1 To 10
MsgBox Me.Controls("CheckBox" & x).Value
Next x


Assuming they are named CheckBox1, CheckBox2, CheckBox3...

Anne Troy
09-28-2004, 10:37 PM
Hey, VA, did you ever get this one resolved?

:)

village_alchemist
09-30-2004, 10:37 AM
Hey, VA, did you ever get this one resolved?

:)Well, yes and no. As DrJ pointed out, when you loop through the controls using the "For Each" statement, they get processed in the order that they were added to the form. This is not a setting that can be changed (except to re-create the form and add the controls in exactly the order you need to process them). The pain of doing this, let alone the anticipated pain of adding something in the middle later on, made me shy away from doing this.

I finally ended up handling two different ways (Ok, I was playing... :) ). For one set of controls I put a number in the .tag property and added the controls to an array with the index being the value of the .tag property. Then I just looped through the array from 1 to n and everything was neatly ordered.

In other cases I just coded multiple If statements in the order I needed them.

So, yes, I'm done. But no, it's not as elegant a solution as I would have liked.

Case closed, on to new projects! :type

Steiner
09-30-2004, 11:22 PM
:bawl I was hoping to get a neat way around that, too. Because I just love For each - loops as they make later additions quite flexible, and I just have the same problem as you do.

Maybe we should send our complaints to Bill, then maybe VB .NET#++ will have an userdefined order available.

Howard Kaikow
09-30-2004, 11:26 PM
:bawl I was hoping to get a neat way around that, too. Because I just love For each - loops as they make later additions quite flexible, and I just have the same problem as you do.

Maybe we should send our complaints to Bill, then maybe VB .NET#++ will have an userdefined order available.

VB .NET does not have Control Arrays as in VB 6, but it does allow for arrays of controls.

village_alchemist
10-01-2004, 06:43 AM
Maybe we should send our complaints to Bill, then maybe VB .NET#++ will have an userdefined order available.
Heck, it's already there! They just need to expose it as a property! I guess we can dream... :grdmartie