简体   繁体   中英

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?

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 .

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属性图片

Try:

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM