0

Context: I have one form which contains a button to open a new one. I'd like to move both of the forms together while moving the first one (so the second one would be stick on the first one). The first form isn't a parent form nor a child one. The second form either.


What have I tried: Moving two forms together without changing the positions (behind or front) of the two forms and How do you attach two forms together in C#? Both of those solutions didn't work for me.


I'm gonna keep searching by my side, but any help would be appreciated, thanks.

DerStarkeBaer
  • 669
  • 8
  • 28
aziui
  • 57
  • 1
  • 9
  • 1
    In what way did the first answer you posted not work? The second says you can't do it but the first one shows a way. Why is it not applicable to you? – Athanasios Kataras Feb 26 '20 at 14:01
  • Well, I get a NullReferenceException that happens on the last line (the one which says that childForm needs to be a class member, actually mine is a class member) – aziui Feb 26 '20 at 14:15
  • Not if you have done this: `this.childForm = new ChildFormClass();` for your parent object. If you have, then it wouldn't. These are the 3 first lines that you have to use. – Athanasios Kataras Feb 26 '20 at 14:16

2 Answers2

1

Assuming that you have MainForm and when moving it you want to move all opened ChildForms, you can use LocationChanged event handler:

// previous MainForm location
private Point m_PreviousLocation = new Point(int.MinValue, int.MinValue);

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

  // If the main form has been moved...
  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;
}

so whenever MainForm moves (changes its Location) we move all ChildForms as well.

Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
1

To make the second form move whenever the first form moves you can save the last location of the first form and whenever the Form.Move event fires, calculate the difference and change the child form accordingly:

public partial class ParentForm : Form
{
    private ChildForm _childForm;
    private Point? _lastLocation;

    ...

    private void ParentForm_Move(object sender, EventArgs e)
    {
        if (_lastLocation != null
            && _childForm != null
            && Application.OpenForms.Cast<Form>().Contains(_childForm))
        {
            _childForm.Location = new Point(
                _childForm.Location.X + Location.X - _lastLocation.Value.X,
                _childForm.Location.Y + Location.Y - _lastLocation.Value.Y);
        }

        _lastLocation = Location;
    }

    ...
}

To make the second form always over the first one while still being able to click and move the first form, you can set the first form as the owner of the second form:

ChildForm = new ChildForm();
ChildForm.Owner = this;
ChildForm.Show();

Or:

ChildForm = new ChildForm();
ChildForm.Show(this);
Eliahu Aaron
  • 4,103
  • 5
  • 27
  • 37