rodsarria
rodsarria

Reputation: 513

How to position the opening form at specific location in C# Windows Forms?

The Location property in the form is set to 0,0 (Properties Window). However, the form doesn't open at the specified location. Am I missing something?

Upvotes: 32

Views: 62647

Answers (4)

Steve
Steve

Reputation: 216243

Setting the Location at 0,0 has no effect if you forget to set StartPosition to FormStartPosition.Manual

This property enables you to set the starting position of the form when it is displayed at run time. The form’s position can be specified manually by setting the Location property or use the default location specified by Windows. You can also position the form to display in the center of the screen or in the center of its parent form for forms such as multiple-document interface (MDI) child forms.

Upvotes: 5

user2509901
user2509901

Reputation:

You need to set StartPosition to manual to make the form set start position to the value in Location Property.

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

Intelisense Summary for FormStartPosition.Manual

FormStartPosition FormStartPosition.Manual

The position of the form is determined by the System.Windows.Forms.Control.Location property.

Upvotes: 67

nitewulf50
nitewulf50

Reputation: 550

By default the start position is set to be WindowsDefaultLocation which will cause the form to ignore the location you are setting. To easily have the set location enforced, change the StartPosition to Manual.

StartPosition Property Picture

Upvotes: 6

Habib
Habib

Reputation: 223187

Try:

this.Location = new Point(Screen.PrimaryScreen.Bounds.X, //should be (0,0)
                          Screen.PrimaryScreen.Bounds.Y);
this.TopMost = true;
this.StartPosition = FormStartPosition.Manual;

Upvotes: 3

Related Questions