Powershell 重新打开子表单

powershell reopening subform

我正在用 PowerShell 编写我的第一个子表单。当我编写代码时,一切都完美地触发了。当我通过 window 中的 x 关闭子表单然后重新打开它时,我收到错误:

异常设置“可见”:“无法访问已释放的对象。 对象名称:'Form'。” 在 line:5 char:5 $ExampleForm.visible = $真 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [], SetValueInvocationException + FullyQualifiedErrorId : ExceptionWhenSetting

我的代码如下:


function TestFunction
{
    $ExampleForm.enabled = $true
    $ExampleForm.visible = $true
}

$Form = New-Object system.Windows.Forms.Form 
$Form.Size = New-Object System.Drawing.Size(1050,425) 
$form.MaximizeBox = $false 
$Form.StartPosition = "CenterScreen" 
$Form.FormBorderStyle = 'Fixed3D' 
$Form.Text = "Test Form"  

$ExampleForm = New-Object system.Windows.Forms.Form 
$ExampleForm.Size = New-Object System.Drawing.Size(550,425) 
$ExampleForm.MaximizeBox = $false 
$ExampleForm.StartPosition = "CenterScreen" 
$ExampleForm.FormBorderStyle = 'Fixed3D'
$ExampleForm.Text = "Example"
$ExampleForm.Visible = $False

$TestButton = new-object System.Windows.Forms.Button
$TestButton.Location = new-object system.drawing.size(401,140) 
$TestButton.Size = new-object system.drawing.size(80,50)
$TestButton.Text = "Test"
$TestButton.Add_Click({TestFunction}) 
$TestButton.TabIndex = 33
$Form.Controls.Add($TestButton)

$Form.ShowDialog()

我做错了什么???

您每次都需要创建一个新表单

function TestFunction
{
  $ExampleForm = New-Object System.Windows.Forms.Form 
  $ExampleForm.Size = New-Object System.Drawing.Size(550,425) 
  $ExampleForm.MaximizeBox = $false 
  $ExampleForm.StartPosition = "CenterScreen" 
  $ExampleForm.FormBorderStyle = 'Fixed3D'
  $ExampleForm.Text = "Example"

  # ShowDialog will prevent re-focusing on parent form until this one closes
  [void]$ExampleForm.ShowDialog()

  # If you don't want this modal behavior, use `Show()` instead:
  # [void]$ExampleForm.Show()
}

$Form = New-Object System.Windows.Forms.Form 
$Form.Size = New-Object System.Drawing.Size(1050,425) 
$Form.MaximizeBox = $false 
$Form.StartPosition = "CenterScreen" 
$Form.FormBorderStyle = 'Fixed3D' 
$Form.Text = "Test Form"  

$TestButton = New-Object System.Windows.Forms.Button
$TestButton.Location = New-Object System.Drawing.Size(401,140) 
$TestButton.Size = New-Object System.Drawing.Size(80,50)
$TestButton.Text = "Test"
$TestButton.add_Click({TestFunction}) 
$TestButton.TabIndex = 33
$Form.Controls.Add($TestButton)

$Form.ShowDialog()