What would cause a ControlCollection to be reordered after Show()?

39 Views Asked by At

I'm at a loss to explain why the items in a ControlCollection would be reordered after a Show() method - maybe some here might know. I'm working on an older application written in VB.NET using .NET 2.0/VS2008 (yuk, I know ... but it's work)

There are no known events that are firing as a result of the Show (e.g., VisibleChanged) so this is unexpected behavior.

Here's the code:

    Friend Sub RefreshAlarmStatus(ByVal conditionID As Integer, ByRef alarmPanel As Panel) Handles m_AlarmPopUp.RefreshAlarm
    ...
    For ControlIndex = 0 To alarmPanel.Controls.Count - 1
        If alarmPanel.Controls(ControlIndex).Tag = conditionID Then
            alarmPanel.Controls(ControlIndex).Show()
            DynamicCommandLabel = alarmPanel.Controls(ControlIndex).Controls.Item(0).Controls.Item(0)
            DynamicInfoLabel = alarmPanel.Controls(ControlIndex).Controls.Item(1)
            DynamicStatusLabel = alarmPanel.Controls(ControlIndex).Controls.Item(0).Controls.Item(1)
            DynamicPanel = alarmPanel.Controls(ControlIndex)
            bFound = True
            Exit For
        End If
    Next

I put a couple of debug lines in the code and got this:

01:27:59.524 - RefreshAlarmStatus: Before Show() - alarmPanel.Controls.Item(0).Tag=2
01:27:59.525 - RefreshAlarmStatus: Before Show() - alarmPanel.Controls.Item(1).Tag=3
01:27:59.525 - RefreshAlarmStatus: Before Show() - alarmPanel.Controls.Item(2).Tag=4
The alarmPanel.Controls(ControlIndex).Show() operation happens here
01:27:59.529 - RefreshAlarmStatus: After Show()  - alarmPanel.Controls.Item(0).Tag=3
01:27:59.529 - RefreshAlarmStatus: After Show()  - alarmPanel.Controls.Item(1).Tag=2
01:27:59.529 - RefreshAlarmStatus: After Show()  - alarmPanel.Controls.Item(2).Tag=4

The weird thing is this happens only some of the time, not all the time. As a result, my use of ControlIndex later on is all snafu'd. I've now changed my code to make the Show independent of the for next and things seem to work.

Should I expect that the ordering of Controls can change after any method - like a Show()? Or is this a bug? Or some other explanation?

0

There are 0 best solutions below