2

I see that if I try to set the form location via:

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

or via the Properties Editor windows (i.e, via the InitializeComponents() method) my desired location is ignored and the window appears (at least on my dev machine) at (156,156) - probably some default location.

However, if I do the same line in the Form_Load() handler - it works just fine.

    private void Form1_Load(object sender, EventArgs e)
    {
        this.Location = new Point(500, 500);
        Debug.WriteLine(this.Location);
    }

So my question is Why? Clearly, (or not clearly) other properties such as Width/Height can be set in the constructor and they seem to work just fine.

(I ask because I teach C#/WinForms and I want to give the correct explanation to the students)

Cœur
  • 37,241
  • 25
  • 195
  • 267
user2795947
  • 53
  • 1
  • 5
  • 1
    StartupPosition = Manual http://stackoverflow.com/questions/10330470/c-sharp-window-positioning – Slai Oct 30 '16 at 12:10
  • 1
    Figuring out where to put a window is an operating system duty on Windows. Pretty much what an OS is supposed to do. Winforms buys into that approach by giving its StartupPosition property the default value, the highly descriptive "WindowsDefaultLocation". The Load event fires *after* the window is created and got its default location, so moving it somewhere else works fine. In it is in general what you *should* do since you don't know yet how large the window is going to be in the constructor. It is *not* equal to Size, especially not today with lots of users changing their DPI setting. – Hans Passant Oct 30 '16 at 14:59

0 Answers0