How do I parent a new window to the current GH document?


(Thomas Wortmann) #1

How do I parent a new window to the current GH document, such that it opens centered on the document, and closes when the document is closed (in C#)?

This is what I have right now, but it doesn’t close the window when the document, or even Grasshopper is closed (I assume because Grasshopper is just hidden):

if (_window == null || _window.IsDisposed)
{
    _window = new Window(this);

}

var owner = Grasshopper.Instances.DocumentEditor;
GH_WindowsFormUtil.CenterFormOnWindow(_opossumWindow, owner, true);
_window.Show(owner);

(David Rutten) #2

Correct, Grasshopper is hidden, not closed unless the GrasshopperUnloadPlugin command is invoked.

The Grasshopper document editor has a FormShepard associated with it and you can register your form with the shepard. It will automatically move your window along with Grasshopper, and also synchronise the hide/show.


(Thomas Wortmann) #3

Thanks David! That’s what I needed!

Another minor thing:
CenterFormOnWindow doesn’t seem to do anything.
The Form always opens in the upper left of the screen, which is fine, I’m just wondering why this line doesn’t seem to have any effect.

var owner = Grasshopper.Instances.DocumentEditor;

if (_window == null || _window.IsDisposed)
{
    _window = new Window(this);
    owner.FormShepard.RegisterForm(_opossumWindow);
}

GH_WindowsFormUtil.CenterFormOnWindow(_opossumWindow, owner, true);
_window.Show(owner);

(David Rutten) #4

I think CenterFormOnWindow method can only work if the Form.StartPosition property is set to FormStartPosition.Manual


(Thomas Wortmann) #5

That did it! Thanks!