Results 1 to 6 of 6

Thread: [RESOLVED] MDI Children Not Respecting Order

  1. #1

    Thread Starter
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,966

    Resolved [RESOLVED] MDI Children Not Respecting Order

    I have a class defined as a readonly property in a form that is an MDI child.

    What I do when an event is fired is:
    1. Loop over the MDI children
    2. If the respective class' IsHidden is false then call Show
    3. Loop over the MDI children again ordered by their respective class' SortOrder
    4. Call BringToFront to set their order


    The reason this is relevant is because if one of the MDI children become maximized, the others are hidden. So the event getting fired is when the maximized child is going back to its normal size and I need to reshow the MDI children that were previously hidden.

    Here is the business logic described above:
    Code:
    Dim formEnteringFullScreen = DirectCast(sender, FormCamera)
    Dim cameraWindows = FlowLayoutPanelCameras.Controls.OfType(Of FormCamera)
    For Each cameraWindow In cameraWindows
        If (cameraWindow Is formEnteringFullScreen) Then
            Continue For
        End If
    
        If (Not cameraWindow.Camera.IsHidden) Then
            cameraWindow.Show()
        End If
    Next
    For Each cameraWindow In cameraWindows.OrderBy(Function(window) window.Camera.SortOrder)
        cameraWindow.BringToFront()
    Next
    The issue is that it doesn't seem like the BringToFront is actually setting the Z-Index properly because if I two children forms, maximize the second, then make it go back to its normal size, the second one now shows up as the first one.

    What I expect to happen is that after every form has been shown, calling BringToFront would change the Z-Index so that the order is what it was prior to the child MDI form being the only one shown.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,670

    Re: MDI Children Not Respecting Order

    Are the SortOrder values what you expect, i.e. is BringToFront being called in the order you expect but not producing the z-order you expect?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,966

    Re: MDI Children Not Respecting Order

    Correct. If I setup a breakpoint on where I'm calling BringToFront, it always executes in the same order. For example:
    1. I open the second form in full screen, which hides all the rest
    2. I click close to trigger the code in post #1
      1. The first form breaks first
      2. The second form breaks second
      3. The second form shows up first and the first form shows up second
    3. I click on the second form (which was originally the first form), which hides all the rest
    4. I click close to trigger the code in post #1
      1. The first form breaks first
      2. The second form breaks second
      3. The second form (which was originally the first form) shows up first and the first form (which was originally the second form) shows up second


    So it doesn't really make sense to me because it appears as though the z-order is completely ignored when calling BringToFront.

    Update - Even doing this produces the same results:
    Code:
    Dim orderedCameras = cameraWindows.OrderBy(Function(window) window.Camera.SortOrder)
    For index = 0 To orderedCameras.Count() - 1
        Dim iteratedCamera = orderedCameras.Skip(index).First()
        FlowLayoutPanelCameras.Controls.SetChildIndex(iteratedCamera, index)
    Next
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  4. #4

    Thread Starter
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,966

    Re: MDI Children Not Respecting Order

    After several exchanges with ChatGPT, I was finally able to get it to work by clearing the FlowLayoutPanels controls and readding them:
    Code:
    Dim cameraWindows = FlowLayoutPanelCameras.Controls.OfType(Of FormCamera)
    Dim orderedWindows = cameraWindows.OrderBy(Function(window) window.Camera.SortOrder).ToArray()
    
    FlowLayoutPanelCameras.SuspendLayout()
    FlowLayoutPanelCameras.Controls.Clear()
    For Each cameraWindow In orderedWindows
        If (Not cameraWindow.Camera.IsHidden) Then
            FlowLayoutPanelCameras.Controls.Add(cameraWindow)
            cameraWindow.Show()
        End If
    Next
    FlowLayoutPanelCameras.ResumeLayout()
    I don't particularly like this approach because ideally the I would like for the controls to remain and just set their z-index, but considering that nothing I tried worked c'est la vie.

    If y'all have any other options, I'm certainly open to them.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,669

    Re: [RESOLVED] MDI Children Not Respecting Order

    Can’t you just minimise the child windows, then restore them descending?

  6. #6

    Thread Starter
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,966

    Re: [RESOLVED] MDI Children Not Respecting Order

    Quote Originally Posted by .paul. View Post
    Can’t you just minimise the child windows, then restore them descending?
    I just tested that and it behaves the same as described in post #3, I'm really at a loss for this.
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width