-1

I'm trying to change Form position to center after changing from a primary screen on a secondary screen

private void Form2_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Space)
        {

             ff = !ff;
             if(ff)
                 showOnScreen(1,this);
             else
                 showOnScreen(0,this); 
        }
    }



void showOnScreen(int screenNumber,Form frm)
    {
        Screen[] screens = Screen.AllScreens;

        if (screenNumber >= 0 && screenNumber < screens.Length)
        {

            Location = screens[screenNumber].WorkingArea.Location;
            this.Location = new Point((screens[screenNumber].Bounds.Size.Width / 2) - (this.Size.Width / 2), (screens[screenNumber].Bounds.Size.Height / 2) - (this.Size.Height / 2));


        }
    }

The form is moved in center of screen but only in my primary screen

daa daa
  • 143
  • 1
  • 2
  • 8
  • 1
    If the form is already on the screen, you will have to measure things yourself and change the Location property. – LarsTech Jun 27 '18 at 18:52
  • add the screen location to it https://stackoverflow.com/a/18496302/1383168 – Slai Jun 27 '18 at 19:11
  • What I meant was, you should have edited your answer to include the link to where the ReallyCenterToScreen function came from. – LarsTech Jun 27 '18 at 19:23

1 Answers1

0

You need to set StartPosition manual of form to set start position value in Location Property.

public Form1()
{
    InitializeComponent();
    this.StartPosition = FormStartPosition.Manual;
    this.Location = new Point(0, 0);
}

Also Take Reference From Here and also check my Another Reference

Now Try to setting WindowStartUpLocation parameter as "manual" inside your showOnScreen method.

Hitesh Anshani
  • 1,499
  • 9
  • 19