Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Showing WinForms dialog with focus from powershell script

One of my coworkers just came to me with an interesting problem.

He's displaying a WinForms form from a PowerShell script, and while the form opens successfully it does not get focus. Instead the PowerShell command window retains focus until the form is explicitly clicked.

The script is being run from the PowerShell command line using .\ScriptName.ps1.

We've tried various combinations of dlg.ShowDialog() (with and without passing $this), dlg.Show(), dlg.Focus(), etc with no luck.

Does anybody know how to give the form focus when it's displayed?

like image 484
Lawrence Johnston Avatar asked Oct 14 '10 18:10

Lawrence Johnston


1 Answers

This is how we got it working (the first line is the one we were missing):

$WinForm.Add_Shown({$WinForm.Activate()})
$WinForm.ShowDialog($this) | out-null
like image 132
Lawrence Johnston Avatar answered Oct 14 '22 11:10

Lawrence Johnston