14

Using Windows Forms I wanted to position window into specific coords. I thought it can be done in a simple way, but following code does not work at all:

public Form1()
{
    InitializeComponent();

    this.Top = 0;
    this.Left = 0;
}

However, when only get a handle to that window, it works well:

public Form1()
{
    InitializeComponent();

    IntPtr hwnd = this.Handle;
    this.Top = 0;
    this.Left = 0;
}

You can see that I am not working with that pointer at all. I found at MSDN following statement:

The value of the Handle property is a Windows HWND. If the handle has not yet been created, referencing this property will force the handle to be created.

Does it mean that we can set window position only AFTER the creation of its handle? Are setters Top/Left using this handle internally? Thank you for clarification.

Shubham Jain
  • 16,610
  • 15
  • 78
  • 125
Fremen
  • 141
  • 1
  • 1
  • 5
  • Not answering your question, but you can override the `OnHandleCreated` method to avoid the call to `this.Handle`. I agree it's weird that `this.Handle` apparently checks for and creates the underlying handle, while other properties dependent on the underlying handle don't. – C.Evenhuis Apr 26 '12 at 09:19
  • did you try to set them in the designer? – Abed Hawa Apr 26 '12 at 09:20
  • I know I can workaround it by many ways. I only wanted to know why it behaves this way because in other frameworks (Qt, wxWidgets, MFC...) you can set your window position directly during the window building. – Fremen Apr 26 '12 at 09:32
  • I have a followup question for you guys.. https://stackoverflow.com/questions/61613124/is-there-a-way-to-get-windows-to-ignore-the-manual-windowstartuplocation-im-usi – Karmel Cohen May 11 '20 at 11:42

4 Answers4

11

Usually a WinForm is positioned on the screen according to the StartupPosition property.
This means that after exiting from the constructor of Form1 the Window Manager builds the window and positions it according to that property.
If you set StartupPosition = Manual then the Left and Top values (Location) set via the designer will be aknowledged.
See MSDN for the StartupPosition and also for the FormStartPosition enum.

Of course this remove the need to use this.Handle. (I suppose that referencing that property you are forcing the windows manager to build immediately the form using the designer values in StartupPosition)

Steve
  • 213,761
  • 22
  • 232
  • 286
5
public Form1()
{
    InitializeComponent();
    Load += Form1_Load;
}

void Form1_Load(object sender, EventArgs e)
{
    Location = new Point(700, 20);
}

Or:

public Form1()
{
    InitializeComponent();
    StartPosition = FormStartPosition.Manual;
    Location = new Point(700, 20);
}
ispiro
  • 26,556
  • 38
  • 136
  • 291
4

You can set the location on form load event like this. this is automatically Handle the Form position.

this.Location = new Point(0, 0); // or any value to set the location
Sunil Acharya
  • 1,153
  • 5
  • 22
  • 39
3

Not very sure about the reason, but if you add the positioning code on the Form_Load event it will work as expected without the need to explicitly initialize the handler.

using System;
using System.Windows.Forms;

namespace PositioningCs
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            /*
            IntPtr h = this.Handle;
            this.Top = 0;
            this.Left = 0;
            */
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.Top = 0;
            this.Left = 0;
        }
    }
}
Carlos Quintanilla
  • 12,937
  • 3
  • 22
  • 25