0

I am quite new to C#. Recently, I got stuck on a problem which is that I want to move two different forms together.

In more detail,

One big form (i.e. something as parent window) popups a small form.

What I want to do is to move these two forms at the same time by moving the parent window.

And, now I am creating the children (small) form by "Show()" method of Form. The problem is that if I click the parent form, a small form goes behind the parent form (i.e. bigger form).

I know this is what should happen. However, I want to move these two forms by moving the bigger form while keeping the small form is front.

I also considered using "ShowDialog()". But, this prevent me from moving the bigger parent. I cannot event touch the parent window.

Blorgbeard
  • 101,031
  • 48
  • 228
  • 272
user2533493
  • 81
  • 2
  • 12

1 Answers1

2

Set the child form's Owner property to the parent form:

this.childForm = new ChildFormClass();
child.Owner = this;
viewForm.ShowDialog();
//Can also be called like this instead of setting Owner property:
//viewForm.ShowDialog(this);

Reference: http://msdn.microsoft.com/en-us/library/system.windows.forms.form.owner.aspx

This way, the child form will never be shown behind the parent.

EDIT:

To move both forms together, simply handle the parent's form Move event:

private void Form1_Move(object sender, EventArgs e)
{
    Point p = this.PointToScreen(new Point(this.ClientRectangle.X, this.ClientRectangle.Y));
    this.childForm.Location = p; //childForm needs to be a class member
}

Cheers

Luc Morin
  • 5,302
  • 20
  • 39