WindowRestorer SetSubWindowRelativeLocation Method (Form, Point, Int32, Int32)CleanCode C# Libraries v1.2.03 API
Sets the location of a sub-window relative to its base form and optionally a parent control on that base form, allowing for multiple monitors.

Namespace: CleanCode.Forms
Assembly: CleanCode (in CleanCode.dll) Version: 1.2.3.0 (1.2.03)
Syntax

public static void SetSubWindowRelativeLocation(
	Form targetForm,
	Point parentRelativeLocation,
	int xOffset,
	int yOffset
)
Remarks

One can set the location relative to a specific parent control of a form or, by passing parentRelativeLocation as (0, 0), relative just to a form. The parentRelativeLocation is typically the Location property of the control displaying the subform, or it may be the Location property of one of its parents--and not necessarily the immediate parent.

A typical usage of this method is inside your subform class. Override the OnVisibleChanged method so that just as the form is being displayed you set its position. That way, the current monitor showing the application will always be the monitor the subform is displayed on. Just before you display the subform with Show or ShowDialog you should set the standard Form.Owner property and optionally the custom ParentRelativeLocation property.

public Point ParentRelativeLocation { get; set; }

protected override void OnVisibleChanged(EventArgs e)
{
    if (Visible)
    {
        WindowRestorer.SetSubWindowRelativeLocation(
            this, Owner, ParentRelativeLocation, 20, 50);
    }
    base.OnVisibleChanged(e);
}

Note that this method is used quite independently of other methods in this class but it is in this class due to being close conceptually.

See Also