I'm trying to call ShowDialog for a WinForm (actual code to create form is located in the .linq script) and all works ok, but the dialog does not show centered in LINQPad application. This is problematic when multiple monitors are in play and I'm staring at LINQPad, run my script and dialog pops up on different monitor. I'm assuming passing in a IWin32Window to ShowDialog will show the dialog centered on the owner, but I don't know how to get the IWin32Window from LINQPad...is it even possible?
The difficulty you'll face in trying to parent the form is that your query runs in a different process to LINQPad.exe.
You can use LINQPad.Util.HostProcessID
to obtain LINQPad's process ID and then Process.GetProcessById (Util.HostProcessID).MainWindowHandle
to get the Windows handle, but then you still won't be able to parent your form. The best you can do is call a Win32 function such as GetWindowRect
to obtain LINQPad's main window position and then manually center your form.
Rather than mess around with this, have you considered just dumping the desired control? LINQPad will then render it in the output pane and start a message loop. For instance:
var panel = new Panel { Padding = new Padding(10) };
panel.Controls.Add (new TextBox { Dock = DockStyle.Top });
panel.Controls.Add (new Label { Dock = DockStyle.Top, AutoSize=true, Text="Testing" });
panel.Dump();
Note that any forms that you then show from within event handlers on your controls will be centered to the parent:
panel.Controls[1].Click += (sender, args) =>
new Form { StartPosition = FormStartPosition.CenterParent }.ShowDialog();