C# – Fixing the position of a form


I am starting a winform application[.NET 3.5, C#], where in the the main form of the application starts at a particular specified location. Am calling the following code in the constructor for this

    private void SetFormPosition()
        this.StartPosition = FormStartPosition.Manual;
        this.Left = Screen.PrimaryScreen.WorkingArea.Right - this.Width;
        this.Top = Screen.PrimaryScreen.WorkingArea.Bottom - this.Height;            

After the application starts, I would like to keep the location of the form fixed throughout the application lifetime.

Perhaps, I could 'tap' the Location event changed but am not sure if that would be very elegant.

Please suggest.


Best Solution

I agree with others that you probably shouldn't be doing this, but if you must, read on.

You can override the SetBoundsCore method and prevent any movement. We use this to prevent vertical resizing on some UserControl implementations (such as those that contain a ComboBox or other fixed height control), but it is also responsible for the location changing.

The following should get you started:

    protected override void SetBoundsCore(
             int x, int y, int width, int height, BoundsSpecified specified)
        x = this.Location.X;
        y = this.Location.Y;
        base.SetBoundsCore(x, y, width, height, specified);