if i do:
panel.control.add(control1)
panel.control.add(control2)
panel.control.add(control3)
...
several times
then do:
dim ctrl as control
for x as integer = panel.control.count-1 to 0 step -1
ctrl = panel.control.item(x)
next
should things come out in the order they went in eg:
panel 1
panel 2
panel 3
panel 1
panel 2
etc...
i dont seem to be getting that...
Thanks!
Dan

panel.control.add
Tim Mercer
Sure, write a simple method such as
Sub MonitorPanels(ByVal p As Panel)
Dim s As String = ""
Dim i As Integer = 0
For i = 0 To p.Controls.Count - 1
Dim c As Control = p.Controls(i)
s = s & i.ToString & " - " & c.Name.ToString & " - " & c.GetType.Name & vbCrLf
Next
MsgBox(s)
End Sub
Which will show you the order and simply call this ...
Luis Esteban Valencia Muñoz
The controls will be listed in the order they were added to the Controls collection unless the collection is modified.
If that loop is removing any items (using Controls.Remove(), Controls.RemoveAt() or Controls.RemoveByKey()) then your results will change.
If any code calls Controls.SetChildIndex() then your results will change.
GertB
You'll probably just want to maintain your own list; maybe a class variable ControList As New List(Of String)
Then whenever you add a control to the form you also add that control's name to the end of the list. When a control is removed, remove it's name from the list. Then you can loop through the list and find the order of controls as they were created.
Surezsu
sooline
I would think that if you are adding and removing items then you would be keeping track of things.
Your original question related to adding items into a collection and them being in a different order to how they were entered. This was not reproducible by a few people and the adding of items into the collection appears to follow the order that they were put in. So it looked like some bug somewhere in your code.
Were you able to get a simple repro scenario - of was this down to removing / adding items from the list.
If you remove items in the middle of the list - this item may be removed and the items might shuffle up now affecting the index - but the index is only an index within the list - with the item remove the index is now correct based upon the current contents in the list.
Eric Wong
So we can agree that the issue was just your code was doing some other actions and that the behaviour of the items appearing in the order that they are added to the collection is working correctly.
SOTY_Programmer
thanks! i was adding and removing an inkedit to the panel in order to speed things up (instead of having dozens inkedits) so this was screwing up the collection.
Michal Konecny
i agree im doing something goofy...is there an easy way to set a watch so i can see all the items in a particular panels controls without having to type each one out individually ex:
instead of
panel.controls(0).text
panel.controls(1).text
panel.controls(2).text
is there a way to do
panel.controls(all).text
or something similar
Thanks,
Dan
Batisse
Form your description I created some code to try it with different controls, creating control in different order and then adding them to the collection.
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'//Order of creating the controls in not sequential
Dim control2 As New Button
control2.Name = "Control2"
Dim control5 As New ComboBox
control5.Name = "Combobox5"
Dim control3 As New Button
control3.Name = "Control3"
Dim control1 As New Button
control1.Name = "Control1"
Dim control4 As New TextBox
control4.Name = "Textbox4"
'//Add to the Panels in Sequential Order
Me.Panel1.Controls.Add(control1)
Me.Panel1.Controls.Add(control2)
Me.Panel1.Controls.Add(control3)
Me.Panel1.Controls.Add(control4)
Me.Panel1.Controls.Add(control5)
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim ctrl As Control
Dim s As String = "BACKWARDS FOR NEXT" & vbCrLf
For x As Integer = Me.Panel1.Controls.Count - 1 To 0 Step -1
ctrl = Me.Panel1.Controls.Item(x)
s = s & ctrl.Name & vbCrLf
Next
s = s & vbCrLf & "FORWARDS FOR NEXT" & vbCrLf
For x As Integer = 0 To Me.Panel1.Controls.Count - 1
ctrl = Me.Panel1.Controls.Item(x)
s = s & ctrl.Name & vbCrLf
Next
s = s & vbCrLf & "ITERATE FOR EACH" & vbCrLf
For Each c As Control In Me.Panel1.Controls
s = s & c.Name & vbCrLf
Next
Textbox1.text = s
End Sub
End Class
With the following code you can see the various ways of iterating through a collection backwards and forwards using a for next or for each loop and the order which they are displayed always reflects the order in which they were added into the collection.
Basically the bold code. Now, something else may be going on in your code as you show only a limited section of code which is not the actual code as this code wont compile as the it should be controls and not control. as we are looking at the collection.
Try this code and making changes to ensure that this appears to work correctly on your machine.
Is the order you are seeing in your application reproducable - ie. if you run the same application 5 times in a row will it always display the same order or does it vary. It sounds as though you need to verify immediately after adding the controls the order of the controls and then later in you application if the order is different - then you need to investigate what is occuring in between that may be effecting the order.
Either way it appears to work - as expected.
ChitownDotNet
Karrar
GovardhanReddy
lsbFirst
txtFirst
txtSecond
lsbFirst
txtFirst
txtSecond
buladbanaw