2

I am writing a simple windows form application. I want that the form will open only in the main screen, no matter in which screen the application was invoked (Assuming that the user has more than one screen). The purpose of the main screen is for the screen that shows the windows taskbar.

Thanks, Maor.

Maor.I
  • 47
  • 9
  • 1
    [Get primary screen](https://stackoverflow.com/q/28506288/1997232) is easy, then just [set form location](https://stackoverflow.com/q/4651909/1997232). – Sinatr Jul 20 '18 at 11:42
  • Hi, @rs232 the question asked there is different from what I ask. I don't want the window will open on the screen from which it invoked, I want that always the window will open on the main screen of the operating system. (with that taskbar) – Maor.I Jul 20 '18 at 11:45
  • Thanks @Sinatr, I think that's what I was looking for. – Maor.I Jul 20 '18 at 11:50
  • 2
    @rs232 lol sort of, the OP in that question already knows how to do what the OP in this question wants. Though the example he gave in the question may answer this question. – Hack Jul 20 '18 at 11:54
  • You can do it with the designer. Ensure the Location property is still (0,0), then just set the StartPosition to Manual. – Hans Passant Jul 20 '18 at 14:33

2 Answers2

5

You can try this:

/// <summary>
/// Sets the location of the form to primary screen.
/// </summary>
/// <param name="this">Instance of the form.</param>
/// <param name="x">The x coordinate of the form's location.</param>
/// <param name="y">The y coordinate of the form's location.</param>
public static void SetPrimaryLocation(this Form @this, int x = 0, int y = 0)
{
    var rect = Screen.PrimaryScreen.WorkingArea;
    @this.Location = new Point(rect.X + x, rect.Y + y);
}
vito
  • 136
  • 5
-1

You should set the IsMdiParent property of your main form to yes.

And when you show other forms, you should show them like this :

    private void MainForm_Load(object sender, EventArgs e)
    {
        Form2 frm = new Form2();
        frm.MdiParent = this;
        frm.Show();
    }

This will open the Form2 in your main form.

Coskun Ozogul
  • 2,389
  • 1
  • 20
  • 32