Powershell:当一个人在彼此之后被召唤时,表格不显示

时间:2015-09-18 13:51:14

标签: forms powershell showdialog

我是初学者所以这个问题可能很愚蠢 我有一个多选列表框的代码。当我运行它,它就行了。我还有另一个选择文件的功能,它也可以独立工作。

然而,当我组合2时(首先是文件选择器然后是列表框),列表框显示为空白,当我将鼠标放在它们上时,项目列表出现。当我在更长的代码中使用它时,PowerShell会被冻结。

有没有人可以给我一个暗示为什么会发生这种情况以及如何解决它? 非常感谢

文件选择器的代码:

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

function Read-OpenFileDialog([string]$WindowTitle, [string]$InitialDirectory, [string]$Filter = "All files (*.*)|*.*", [switch]$AllowMultiSelect) 
         {     
          $openFileDialog = New-Object System.Windows.Forms.OpenFileDialog     
          $openFileDialog.Title = $WindowTitle     
          if (![string]::IsNullOrEmpty($InitialDirectory)) { $openFileDialog.InitialDirectory = $InitialDirectory } 
          $openFileDialog.Filter = $Filter    
          if ($AllowMultiSelect) { $openFileDialog.MultiSelect = $true }     
          $openFileDialog.ShowHelp = $true    
          # Without this line the ShowDialog() function may hang depending on system configuration and running from console vs. ISE.     
          $openFileDialog.ShowDialog() > $null    
          if ($AllowMultiSelect) { return $openFileDialog.Filenames } else { return $openFileDialog.Filename }
          $openFileDialog.Close()
          } 
$filePath = Read-OpenFileDialog -WindowTitle "Select the file" -InitialDirectory '~' -Filter "Project Files (*.txt)|*.txt"

项目列表框的代码:

$x = @()
$objForm = New-Object System.Windows.Forms.Form 
$objForm.Text = "Data Entry Form"
$objForm.Size = New-Object System.Drawing.Size(300,200) 
$objForm.StartPosition = "CenterScreen" 
if (![string]::IsNullOrEmpty($InitialDirectory)) { $openFileDialog.InitialDirectory = $InitialDirectory } 

$objForm.KeyPreview = $True

$objForm.Add_KeyDown({if ($_.KeyCode -eq "Enter") 
    {
        foreach ($objItem in $objListbox.SelectedItems)
            {$x += $objItem}
            $objForm.Close()
    }
    })

$objForm.Add_KeyDown({if ($_.KeyCode -eq "Escape") {$objForm.Close()}})

$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Size(75,120)
$OKButton.Size = New-Object System.Drawing.Size(75,23)
$OKButton.Text = "OK"

$OKButton.Add_Click(
   {
        foreach ($objItem in $objListbox.SelectedItems)
            {$x += $objItem}
        $objForm.Close()
   })

$objForm.Controls.Add($OKButton)

$CancelButton = New-Object System.Windows.Forms.Button
$CancelButton.Location = New-Object System.Drawing.Size(150,120)
$CancelButton.Size = New-Object System.Drawing.Size(75,23)
$CancelButton.Text = "Cancel"
$CancelButton.Add_Click({$objForm.Close()})
$objForm.Controls.Add($CancelButton)

$objLabel = New-Object System.Windows.Forms.Label
$objLabel.Location = New-Object System.Drawing.Size(10,20) 
$objLabel.Size = New-Object System.Drawing.Size(280,20) 
$objLabel.Text = "Please make a selection from the list below:"
$objForm.Controls.Add($objLabel) 

$objListbox = New-Object System.Windows.Forms.Listbox 
$objListbox.Location = New-Object System.Drawing.Size(10,40) 
$objListbox.Size = New-Object System.Drawing.Size(260,20) 

$objListbox.SelectionMode = "MultiExtended"

[void] $objListbox.Items.Add("Item 1")
[void] $objListbox.Items.Add("Item 2")
[void] $objListbox.Items.Add("Item 3")
[void] $objListbox.Items.Add("Item 4")
[void] $objListbox.Items.Add("Item 5")

$objListbox.Height = 70
$objForm.Controls.Add($objListbox) 
$objForm.Topmost = $True

$objForm.Add_Shown({$objForm.Activate()})
[void] $objForm.ShowDialog()

$x

0 个答案:

没有答案