Codex
Codex

Reputation: 1022

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.

Thanks.

Upvotes: 2

Views: 7964

Answers (3)

Ali Jarod
Ali Jarod

Reputation: 1

Just change this

Location = new Point(this.Width,this.Height);

Upvotes: 0

Jeff Yates
Jeff Yates

Reputation: 62367

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;
        //...etc...
        base.SetBoundsCore(x, y, width, height, specified);
    }

Upvotes: 2

Misko
Misko

Reputation: 2044

You could set the FormBorderStyle to None. This has the added benefit of removing the bar at the top of the window that would give users a false sense that they should be able to move the window.

Upvotes: 2

Related Questions