0

This sounds so simple that it may already been asked, but I could not find the same question (sorry if it's there).

I have a form ("owner") maximized with various "owned" children (smaller) forms which are either minimized or in "normal" state.

If I minimize the (owner) form and then restore it to its initial window state (maximized), I can see that all the owned children also get (strangely) automatically to a "normal" window state.

Instead I want to keep them the same way as I left them when I minimized the owner form, because this is just what a user would expect.

How do I correct this behavior. Is there any settings at design time to avoid that ? (Any example/help in either c# or vb.net would be great.)

Pam
  • 474
  • 1
  • 4
  • 13
  • 1
    see http://stackoverflow.com/q/18234312/1070452 seems like it could be done capturing the state and/or size in various events, but maybe not – Ňɏssa Pøngjǣrdenlarp Apr 30 '14 at 16:11
  • 1
    Add an event handler for the Resize event in one of those misbehaving children. Set a breakpoint on it, beware that it will fire multiple times so just keep pressing F5. Reproduce the problem, post the content of the Call Stack window in your question. – Hans Passant Apr 30 '14 at 17:23
  • Ok @Hans Passant. Did that with one children (they are all the same). When I click on the maximize button of the owner, the first thing I see is the children to pop up to normal state. Than the owner maximizes. The stack content of the resize event contains nothing special: only one line (External Code): MyProg.exe!PlayerInfoView.PlayerInfoView_Resize(Object sender, System.EventArgs e) Line 356 – Pam Apr 30 '14 at 17:41
  • btw, It it can be of any relevance, these children have the maximize button disabled (by design). (They are just smaller tool windows, not meant to be maximized.) – Pam Apr 30 '14 at 17:50

1 Answers1

1
Public Class Form1

Private _previousWindowState As FormWindowState = FormWindowState.Normal

Private Sub Form1_Resize(sender As Object, e As EventArgs) Handles Me.Resize

If _previousWindowState <> FormWindowState.Minimized AndAlso Me.WindowState = FormWindowState.Minimized Then
    For Each child As Form In Me.OwnedForms
        child.Tag = child.WindowState
    Next
ElseIf _previousWindowState = FormWindowState.Minimized AndAlso Me.WindowState <> FormWindowState.Minimized Then
    Me.BeginInvoke(New Threading.ThreadStart(AddressOf FixChildren))
End If

_previousWindowState = Me.WindowState
End Sub

Private Sub FixChildren()
For Each child As Form In Me.OwnedForms
    child.WindowState = CType(child.Tag, FormWindowState)
Next
End Sub

End Class
ileff
  • 358
  • 2
  • 5
  • Thank you @ileff. So, the "hard way" is needed :-) I was just hoping that such a seemingly simple issue was addressable at design time through some simple property, as it seemed the most intuitive way to behave. (Wondering why the children behave like that, anyway.) – Pam May 01 '14 at 09:25
  • It seems to work fine. Does child.WindowState = CType(child.Tag, FormWindowState) require a security check for possibly disposed children (.isdisposed() ) or the children in Me.OwnedForms cannot ever be in such a state ? – Pam May 01 '14 at 11:59
  • 1
    When a child form is closed it is automatically removed from the parent's OwnedForms array – ileff May 01 '14 at 15:13