views:

359

answers:

2

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.

+1  A: 

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);
    }
Jeff Yates
+1  A: 

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.

CodeMonkey1
Umm... am not sure if the client will be happy. We show some vital information in the title bar!!
Codex
This information couldn't be put in a label at the top of the form?
CodeMonkey1