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?
CodePudding user response:
As far as I know you have 3 options:
Use FormStartPosition.CenterScreen;
private void button2_Click(object sender, EventArgs e) { Form2 child = new Form2(); child.StartPosition = FormStartPosition.CenterScreen; child.ShowDialog(this); }Use FormStartPosition.CenterParent
private void button2_Click(object sender, EventArgs e) { Form2 child = new Form2(); child.StartPosition = FormStartPosition.CenterParent; child.ShowDialog(this); }}
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); }
CodePudding user response:
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();
- First I query which screen the child form want to open
- Then I query which screen my Main form is currently open
- Then I over right the child form screen
