Home > Mobile >  Child form opens on first screen when user has dragged the main form to the second screen
Child form opens on first screen when user has dragged the main form to the second screen

Time:02-01

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:

  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);
     }
    

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();
  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 over right the child form screen
  •  Tags:  
  • Related