2

My Application's Main Form opens on the Laptop (main screen) when it starts. Then user drags it to the other screen (for big display), and opens a child form that is displayed on the Laptop screen rather than Application's Main Form (big display). I want the child form to open on the screen where Application's Main Form is open at the moment.

I tried following options, but they only worked in debug mode and did not work in production

ChildForm.ShowDialog((IWin32Window)this.MainForm);
ChildForm.ShowDialog(formMainInstance);
ChildForm.Show(formMainInstance);

I know about FormStartPosition.CenterParent, but it's not the right option for me. How can I do this?

LarsTech
  • 80,625
  • 14
  • 153
  • 225
Tim
  • 582
  • 2
  • 6
  • 12
  • When do you set the FormStartPosition to CenterParent? (in code or in the designer) – Steve Jan 21 '22 at 14:53
  • I dont want to use FormStartPosition to CenterParent because child form opens on top left corner – Tim Jan 21 '22 at 14:55
  • What is this.MainForm? What is this here? What is formMainInstance? What are these coming from? -- The new Form should open where the Owner is located. How should it open on top left corner? You're not setting anything that causes that, unless you have just set `StartPosition` to `Manual` without specifying *where* to start from. -- Could be useful: [Using SetWindowPos with multiple monitors](https://stackoverflow.com/a/53026765/7444103) – Jimi Jan 21 '22 at 15:39
  • In other *words*, you could set: `var screen = Screen.FromControl(this); ChildForm.Location = screen.WorkingArea.Location; ChildForm.Show(this);` where `this` is the Owner Form and `StartPosition` is set to `Manual`. – Jimi Jan 21 '22 at 15:42
  • @Jimi this.MainForm is Windows Form property ApplicationContext.MainForm that gets or sets the form to use as context. however formMainInstance is the instance of my mainform – Tim Jan 21 '22 at 15:43

2 Answers2

0

As far as I know you have 3 options:

  1. Use FormStartPosition.CenterScreen;

         private void button2_Click(object sender, EventArgs e)
     {          
         Form2 child = new Form2();
         child.StartPosition = FormStartPosition.CenterScreen;
         child.ShowDialog(this);
     }
    
  2. Use FormStartPosition.CenterParent

    private void button2_Click(object sender, EventArgs e)
     {          
         Form2 child = new Form2();
         child.StartPosition = FormStartPosition.CenterParent;
         child.ShowDialog(this);
     }
    

    }

  3. Use FormStartPosition.Manual and pass a point

        private void button2_Click(object sender, EventArgs e)
     {          
         Form2 child = new Form2();
         child.StartPosition = FormStartPosition.Manual;
         child.Location = new System.Drawing.Point(0, 0);
         child.ShowDialog(this);
     }
    
Aviv Halevy
  • 161
  • 1
  • 10
0

The following code worked for me.

var screen = Screen.FromControl(childFormInstance);
var MainFormScreen = Screen.FromControl(_formMainInstance);
ChildForm.Left = MainFormScreen.WorkingArea.Left + 120;
ChildForm.Top = MainFormScreen.WorkingArea.Top + 120;
ChildForm.ShowDialog();
  1. First I query which screen the child form want to open
  2. Then I query which screen my Main form is currently open
  3. Then I overwrite the child form screen
Tim
  • 582
  • 2
  • 6
  • 12