Maor.I
Maor.I

Reputation: 47

How to make the windows form open only on the main screen

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.

Upvotes: 2

Views: 1844

Answers (2)

Coskun Ozogul
Coskun Ozogul

Reputation: 2469

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.

Upvotes: -1

vito
vito

Reputation: 136

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

Upvotes: 5

Related Questions