DeveloperSD
DeveloperSD

Reputation: 129

Display Form on centre of screen for a dual monitor How to?

I am developing a Windows desktop application dual monitor where I need to display my form sometimes on primary screen and sometimes on secondary, which works fine but when I display it on my secondary screen I want it to be displayed on centre of my screen which is not working.

Here is my code:

if (Screen.AllScreens.Length > 1)
myForm.Location = Screen.AllScreens[1].WorkingArea.Location;
myForm.StartPosition = FormStartPosition.Manual; // because i wrote manual it is displayed on Top left of my secondaryScreen which is ok
myForm.show();

but I want to diplay it on centre so I wrote

myForm.StartPosition = FormStartPosition.CentreScreen;
//it is not working again a form is displayed on Centre of PrimaryScreen..

Any idea why?

Upvotes: 2

Views: 5269

Answers (4)

Mehdi
Mehdi

Reputation: 2283

    private void CenterOnTheCurrentScreen()
    {
        Rectangle workingArea = Screen.FromControl(this).WorkingArea;
        Point center = new Point((workingArea.Width - this.Width) / 2, (workingArea.Height - this.Height) / 2);

        this.Location = new Point(workingArea.X + center.X, workingArea.Y + center.Y);
    }

Upvotes: 0

Bruno Saboia
Bruno Saboia

Reputation: 352

You could write an extension method:

public static void MoveForm(this Form form, Screen screen = null)
{
    if(screen == null)
    {
        //If we have a single screen, we are not moving the form
        if(Screen.AllScreens.Length > 1) return;

        screen = Screen.AllScreens[1];
    }
    var bounds = screen.Bounds;

    form.Left = ((bounds.Left + bounds.Right) / 2) - (form.Width / 2);
    form.Top = ((bounds.Top + bounds.Bottom) / 2) - (form.Height / 2);            
}

Upvotes: 0

Hans Passant
Hans Passant

Reputation: 942119

You cannot use StartPosition.CenterScreen because that picks the monitor on which the mouse is currently located. Usually desirable but not what you are asking for. You must use the form's Load event to move it where you want it. Using form's Load is important, you do not know the size of the window until after it is created and the user's preferences are applied and it is rescaled to match the video DPI.

Boilerplate code should look like this:

    private void button1_Click(object sender, EventArgs e) {
        var form = new Form2();
        form.Load += CenterOnSecondMonitor;
        form.Show();
    }

    private void CenterOnSecondMonitor(object sender, EventArgs e) {
        var form = (Form)sender;
        var area = Screen.AllScreens.Length > 1 ? Screen.AllScreens[1].WorkingArea : Screen.PrimaryScreen.WorkingArea;
        form.Location = new Point((area.Width - form.Width) / 2, (area.Height - form.Height) / 2);
        form.Load -= CenterOnSecondMonitor;
    }

Or you put this code into the form itself, the more common choice:

    protected override void OnLoad(EventArgs e) {
        var area = Screen.AllScreens.Length > 1 ? Screen.AllScreens[1].WorkingArea : Screen.PrimaryScreen.WorkingArea;
        this.Location = new Point((area.Width - this.Width) / 2, (area.Height - this.Height) / 2);
        base.OnLoad(e);
    }

Upvotes: 2

look for the property of your winform named StartPosition then set it into Center Screen

Upvotes: 0

Related Questions