1

So 2 days ago I asked how to move 2 forms together, I got this answer which really helped me.
But now I would like to know how to move the first form, while the second one is minimized (so it has to stop moving while minimized, or it will result in a crash from the second form...)?
I tried this:

private void MainForm_LocationChanged(object sender, EventArgs e) {
  // All open child forms to be moved
  Form[] formsToAdjust = Application
    .OpenForms
    .OfType<ChildForm>()
    .ToArray();
    FormsToAdjust formsToAdjust1 = new FormsToAdjust(); //added this

  // If the main form has been moved...
if (formsToAdjust1.WindowState != FormWindowState.Minimized) //and this statement as well
{
  if (m_PreviousLocation.X != int.MinValue)
    foreach (var form in formsToAdjust) //... we move all child froms aw well
      form.Location = new Point(
        form.Location.X + Location.X - m_PreviousLocation.X,
        form.Location.Y + Location.Y - m_PreviousLocation.Y
      );

  m_PreviousLocation = Location;
}
}

but it can't work as it will open the second form each time the main form will be moved (yes this was a stupid attempt, but I really can't manage how to go through that problem)...

So the goal would be to, if the second form is minimized, keep being able to move the main form, without changing the location of the second one.

Any help would be really appreciated, I'm going to continue searching by my side (actually searching for a while now) while waiting for a reply.

Thanks :)

aziui
  • 57
  • 1
  • 9
  • why not check if the child form is **minimized** ? If yes, leave it! – Kaj Feb 28 '20 at 14:01
  • 2
    `if (form.WindowState == FormWindowState.Normal) form.Location = ...` – Dmitry Bychenko Feb 28 '20 at 14:05
  • This is not exactly how this thing works. The Child Form(s) should subscribe to the Owner Form `Resize` and `Move` events, checking whether their `WindowState = FormWindowState.Normal` before attempting to move. Also check the `FormWindowState.Maximized` of the Owner Form and reposition accordingly (maybe inside the Owner bounds). You should show a child Form with `.Show(this)`, so a `FormWindowState.Minimized` of the Owner is irrelevant, since a Child Form will also minimize when the Owner is minimized. Or use something like [this](https://stackoverflow.com/a/48812831/7444103). – Jimi Feb 28 '20 at 15:05

1 Answers1

3

You can add a filter with a help of Where: we want to move all child forms that are in Normal (neither Minimized nor Maximized) window state

Form[] formsToAdjust = Application
  .OpenForms
  .OfType<ChildForm>()
  .Where(child => child.WindowState == FormWindowState.Normal)
  .ToArray();

then business as usual:

  if (m_PreviousLocation.X != int.MinValue)
    foreach (var form in formsToAdjust) 
      form.Location = new Point(
        form.Location.X + Location.X - m_PreviousLocation.X,
        form.Location.Y + Location.Y - m_PreviousLocation.Y
      );

  m_PreviousLocation = Location;
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
  • according to https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.form.windowstate?view=netframework-4.8 `Remarks Before a form is displayed, the WindowState property is always set to FormWindowState.Normal, regardless of its initial setting. This is reflected in the Height, Left, Top, and Width property settings. If a form is hidden after it has been shown, these properties reflect the previous state until the form is shown again, regardless of any changes made to the WindowState property.` Isn't it better to check if minimized ? – Kaj Feb 28 '20 at 14:13
  • @Kaj: since we get `Application.OpenForms` so when form is *not displayed* we don't move them – Dmitry Bychenko Feb 28 '20 at 14:15
  • I missed that :) sorry – Kaj Feb 28 '20 at 14:21