Reputation: 225
I am coding a WinForms application in Visual Studio C# 2010 and I want to find out the location of the upper left corner of the WinForm window (the starting location of the window).
How can I do that?
Upvotes: 18
Views: 68459
Reputation: 115
check this: Form.DesktopLocation Property
int left = this.DesktopLocation.X;
int top = this.DesktopLocation.Y;
Upvotes: 6
Reputation: 79
I had a similar case where for my Form2 i needed the screen location of Form1. i Solved it by passing the screen locationof form1 to form2 through its constructor :
//Form1
Point Form1Location;
Form1Location = this.Location;
Form2 myform2 = new Form2(Form1Location);
myform2.Show();
//Form2
Point Form1Loc;
public Form2(Point Form1LocationRef)
{
Form1Loc = Form1LocationRef;
InitializeComponent();
}
Upvotes: 1
Reputation: 23833
If you are accessing it from within the form itself then you can write
int windowHeight = this.Height;
int windowWidth = this.Width;
to get the width and height of the window. And
int windowTop = this.Top;
int windowLeft = this.Left;
To get the screen position.
Otherwise, if you launch the form and are accessing it from another form
int w, h, t, l;
using (Form form = new Form())
{
form.Show();
w = form.Width;
h = form.Height;
t = form.Top;
l = form.Left;
}
I hope this helps.
Upvotes: 21
Reputation: 1592
Use Form.Bounds.Top
to get the "Y" coordinate and Form.Bounds.Left
to get the "X" coordinate
Upvotes: 1
Reputation: 476
Also a combination of the Left and Top properties (e.g. this.Top from within the form)
Upvotes: 0
Reputation: 902
Form.Location.X
and Form.Location.Y
will give you the X and Y coordinates of the top left corner.
Upvotes: 7