6

When I run the following, PowerShell hangs waiting for the dialog to close, even though the dialog is never displayed:

[void] [Reflection.Assembly]::LoadWithPartialName( 'System.Windows.Forms' )
$d = New-Object Windows.Forms.OpenFileDialog
$d.ShowDialog( )

Calling ShowDialog on a Windows.Forms.Form works fine. I also tried creating a Form and passing it as the parent to $d.ShowDialog, but the result was no different.

Emperor XLII
  • 13,014
  • 11
  • 65
  • 75
  • If anyone's interested, this is what I was using it for: http://stackoverflow.com/questions/182573/powershell-cli-or-gui-which-do-you-need-or-prefer#216936 – Emperor XLII Oct 19 '08 at 20:42

3 Answers3

16

I was able to duplicate your problem and found a workaround. I don't know why this happens, but it has happened to others.

If you set the ShowHelp property to $true, you will get the dialog to come up properly.

Example:

[void] [Reflection.Assembly]::LoadWithPartialName( 'System.Windows.Forms' )
$d = New-Object Windows.Forms.OpenFileDialog
$d.ShowHelp = $true
$d.ShowDialog( )

Good Luck!

Steven Murawski
  • 10,959
  • 41
  • 53
  • Interesting workaround! Would be interesting to know how setting ShowHelp "corrects" the issue, but I'm glad it works :) – Emperor XLII Oct 19 '08 at 18:51
  • Strange, the workaround doesn't work for me. With or without it, the dialog opens behind the powershell window. – Charlie Oct 19 '08 at 18:52
  • Looks like the difference might be due to Powershell version or OS version (see comments from Steven on my response) – Charlie Oct 19 '08 at 20:36
  • For me the dialog was never shown; not just hidden behind a window. Adding .ShowHelp = $true worked for me, and I'm using it in my powershell functions for getting different types of input from the user via a GUI (http://blog.danskingdom.com/powershell-multi-line-input-box-dialog-open-file-dialog-folder-browser-dialog-input-box-and-message-box/). Thanks. – deadlydog May 17 '13 at 20:35
2

It appears to me that the dialog is actually opening just fine, but it's behind the powershell console window. Unfortunately it doesn't show in the taskbar, so there's no indication that it's there unless you move the powershell window or Alt+Tab. It also appears that the ShowHelp workaround didn't have any effect for me.

EDIT Here's a way to do do it using your secondary-form idea. The basic idea is to create a new form which opens the OpenFileDialog from inside its Shown event. The key is calling Activate on the form before opening the dialog, so that the form comes to the front and the dialog appears. I moved the form offscreen by setting the Location to an offscreen value, but you could alternatively set Form.Visible = $false from inside the Shown event.

[void] [Reflection.Assembly]::LoadWithPartialName( 'System.Windows.Forms' )

$ofn = New-Object System.Windows.Forms.OpenFileDialog

$outer = New-Object System.Windows.Forms.Form
$outer.StartPosition = [Windows.Forms.FormStartPosition] "Manual"
$outer.Location = New-Object System.Drawing.Point -100, -100
$outer.Size = New-Object System.Drawing.Size 10, 10
$outer.add_Shown( { 
   $outer.Activate();
   $ofn.ShowDialog( $outer );
   $outer.Close();
 } )
$outer.ShowDialog()
Charlie
  • 44,214
  • 4
  • 43
  • 69
  • 1
    I double-checked and the dialog is not appearing at all that I can tell, even after moving or minimizing the powershell window. Must be something different in your configuration? – Emperor XLII Oct 19 '08 at 18:55
  • Tried your edited code; the form showed up but the file dialog only worked after I re-did it with $ofn.ShowHelp set to true. – Emperor XLII Oct 19 '08 at 18:59
  • I wonder if OS could have anything to do with it. I'm using XP Pro - what about you two? – Charlie Oct 19 '08 at 19:34
  • @Charlie I was trying it with Vista (and CTP2) and I just tried it on XP with V1. On XP, I'm getting the same results as you Charlie, but on Vista, I'm getting the "proper" behavior. – Steven Murawski Oct 19 '08 at 20:01
  • 2
    I suggest using this line instead: `$outer.DialogResult = $ofn.ShowDialog( $outer );`. Otherwise, the dialog always returns 'Cancel'. – Hamish Grubijan Aug 22 '11 at 23:20
1

Apparently this has something to do with Multi-Threaded Apartment (MTA) mode. It appears to work fine in Single-Threaded Apartment (-STA) mode.

See also: Could you explain STA and MTA?

Community
  • 1
  • 1
iRon
  • 20,463
  • 10
  • 53
  • 79