0

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?

HiDefLoLife
  • 555
  • 1
  • 8
  • 28
  • are you adding these things dynamically at runtime, or are they designer added? can we assume `Show` is a Form.Show (vs a usercontrol) – Ňɏssa Pøngjǣrdenlarp May 23 '14 at 21:21
  • The controls have been added dynamically during the operation of the program - but not at the time of this search - the Add happens earlier on in the program. And yes, this is a System.Windows.Forms.Show(). – HiDefLoLife May 23 '14 at 21:23
  • are you saying that the 4 `DynamicXXXXXX = ` lines after that `.Show` are snafu or later in the code? – Ňɏssa Pøngjǣrdenlarp May 23 '14 at 21:33
  • Sorry, it isn't easy to see, but if you scroll right on the second section of code I submitted (which is from a Debug log file), you'll see that the Item(x) are reordered immediately after the Show() call - this is visible by looking at the Tag text for each Item. I can't explain why this happens. – HiDefLoLife May 23 '14 at 21:48
  • I am going the bet that the "sometimes" it happens is the first time the matching one is Shown for the first time? Might that be the case? – Ňɏssa Pøngjǣrdenlarp May 23 '14 at 21:52
  • Good guess - but that doesn't appear to be the case. I just ran it to test again and initially Tag=4 is shown (no reordering), then Tag=3 (this is where it gets reordered) and then finally Tag=2 (no reordering). – HiDefLoLife May 23 '14 at 22:07
  • I think it has to do with control creation (guessing). Some things dont exist until they become visible (see HandleCreated on MSDN). It might be that that the **first time** one of these things is Shown parts or all of it are (re)created and (re)added to the controls collection. As these critters appear to have child `Controls()`, that might provide additional opportunity. You could write a `GetCtlForCondition(condID) As alarmThing` function to find the matching one and return an object reference to avoid the problem. – Ňɏssa Pøngjǣrdenlarp May 23 '14 at 23:23
  • What I did to fix this is treat the search independent of the Show() and it works for me. It means two for-next loops, but, importantly, it works consistently. For anyone reading this in the future: It appears that the consensus of feedback is that initial visibility change might be the underlying part of the reordering of the ControlCollection - maybe this is true, my debugging and logging didn't necessarily reflect this theory, but it is a conceivable explanation. Thanks everyone for their input. – HiDefLoLife May 23 '14 at 23:43

0 Answers0