ControlCollection sequence?

  • Thread starter Thread starter Steve
  • Start date Start date
S

Steve

I have a number of controls within a Panel. If I do

For Each ctrl In PanelMain.Controls
MsgBox(ctrl.Name)
Next

The controls are not displayed in the sequence I would expect them to
be (top left to bottom right). The TabIndex properties are properly
set for my expected sequence, but they actually get references in the
reverse order. Is there any rhyme or reason behind how For Each works
and/or how can I get my For Each to go in the sequence I need? Ideas?
TIA... Steve
 
Steve said:
I have a number of controls within a Panel. If I do

For Each ctrl In PanelMain.Controls
MsgBox(ctrl.Name)
Next

The controls are not displayed in the sequence I would expect them
to be (top left to bottom right). The TabIndex properties are
properly set for my expected sequence, but they actually get
references in the reverse order. Is there any rhyme or reason
behind how For Each works

The order is the Z-order. ("send to back", "send to front")
and/or how can I get my For Each to go in
the sequence I need? Ideas?

Put them in an array and sort.

dim c as control()

redim c(me.controls.count - 1)
me.controls.copyto(c, 0)
array.sort(New MyComparer)


class MyComparer
implements System.Collections.IComparer

Public overrides Function Compare( _
ByVal x As Object, ByVal y As Object) As Integer

'compare controls here (by position as you wrote)

end function
end class


Armin
 
Steve said:
I have a number of controls within a Panel. If I do

For Each ctrl In PanelMain.Controls
MsgBox(ctrl.Name)
Next

The controls are not displayed in the sequence I would expect them to
be (top left to bottom right). The TabIndex properties are properly
set for my expected sequence, but they actually get references in the
reverse order. Is there any rhyme or reason behind how For Each works
and/or how can I get my For Each to go in the sequence I need? Ideas?

I assume that 'For...Each' will enumerate the controls in the order they
have been added to the controls collection.
 
Steve said:
I have a number of controls within a Panel. If I do

For Each ctrl In PanelMain.Controls
MsgBox(ctrl.Name)
Next

The controls are not displayed in the sequence I would expect them to
be (top left to bottom right). The TabIndex properties are properly
set for my expected sequence, but they actually get references in the
reverse order. Is there any rhyme or reason behind how For Each works

Yes, but it's a private implementation detail, so as far as we are
concerned, no :)

In other words: The Controls collection implements IEnumerable, which
means we can For Each over it in VB. Implementing IEnumerable means
returning an IEnunmerator when asked. All an IEnumerator is *required*
to do is present each element of the collection once and once only, in
response to repeeated calls to MoveNext. So long as it does that, it
has fulfilled its interface contract - we are not entitled to ask for
any more than that.
and/or how can I get my For Each to go in the sequence I need? Ideas?

The ControlCollection returned by Controls implements IList as well,
which is the interface that has the semantics of an _indexed_ list.
It's obviously an ICollection as well, which means it has a Count. So
to iterate through it in order of the items' indexes, you can use a
normal For Next loop:

Dim ctrl As Control
Dim cc As ControlCollection = PanelMain.Controls
For i As Integer = 0 To cc.Count - 1
ctrl = cc(i)
' do stuff with ctrl
Next

But you probably knew that anyway.

In short: The semantics of For Each specifically do not provide an
ordering - this makes IEnumerable very easy to implement, and indeed
makes it possible to implement where an ordering might actually be
impossible to define.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top