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.
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.
I preferred this:
myModalForm.Location = New Point(myControl.PointToScreen(Point.Empty).X + myControl.Width, myControl.PointToScreen(Point.Empty).Y)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With