Reputation: 6283
Well, I have a form that I open using:
ShowDialog(this);
I try to change the position of the form using its Location
property, but I don't understand what exactly is this position relative to? I want to open this form below a certain button. So how can this be done?
Thanks.
Upvotes: 7
Views: 12187
Reputation: 66604
A Form will expect the co-ordinates relative to the screen’s top-left corner. However, the location of a Control within a Form is relative to the top-left corner of the form†.
Use the Control’s Location
property to find its location, and then call PointToScreen
on the Form object to turn it into screen co-ordinates. Then you can position the new form relative to that.
For example:
var locationInForm = myControl.Location;
var locationOnScreen = mainForm.PointToScreen(locationInForm);
using (var model = new ModelForm())
{
model.Location = new Point(locationOnScreen.X, locationOnScreen.Y + myControl.Height + 3);
model.ShowDialog();
}
† Actually the top-left corner of the client area of the form.
Upvotes: 10
Reputation: 1262
I preferred this:
myModalForm.Location = New Point(myControl.PointToScreen(Point.Empty).X + myControl.Width, myControl.PointToScreen(Point.Empty).Y)
Upvotes: 5