PowerShell调用新的.NET表单

时间:2015-01-05 12:34:29

标签: .net forms powershell

我试图让我的脚本可供最终用户使用。为此,他们需要能够单击某些按钮来启动我们拥有的一些脚本。理想情况下,.NET表单会显示不同的类别,选择类别后会显示正确的操作按钮。

我遇到的问题是 #Call Sub menu 1 部分,之前的表单未关闭且保持可见(当您将表单移到顶部时,可以看到这一点)。为什么$MenuBox.Close()无法摆脱初始窗口?

是否有更好的方法可以通过这种方式制作菜单?或者每次需要查看新窗口时是否有义务创建新的Form?在稍后阶段,我需要添加后退按钮以进行导航。

代码:

[void] [System.Reflection.Assembly]::LoadWithPartialName('System.Windows.Forms')
[void] [System.Reflection.Assembly]::LoadWithPartialName('System.Drawing')
[void] [System.Windows.Forms.Application]::EnableVisualStyles()
$WindowTitle = 'Script center'
$Title = 'Welcome to Script Center'

# Main menu
$MenuBox = New-Object System.Windows.Forms.Form
$MenuBox.Size = New-Object System.Drawing.Size @(650,450)
$MenuBox.Text = $WindowTitle
$MenuBox.StartPosition = "CenterScreen"
$MenuBox.AutoSize = $False
$MenuBox.FormBorderStyle = [System.Windows.Forms.FormBorderStyle]::Fixed3D
$Icon = [system.drawing.icon]::ExtractAssociatedIcon($PSHOME + "\powershell.exe")
$MenuBox.Icon = $Icon

# AD menu
$ADBox = New-Object System.Windows.Forms.Form
$ADBox.Size = New-Object System.Drawing.Size @(650,450)
$ADBox.Text = $WindowTitle
$ADBox.StartPosition = "CenterScreen"
$ADBox.AutoSize = $False
$ADBox.FormBorderStyle = [System.Windows.Forms.FormBorderStyle]::Fixed3D
$Icon = [system.drawing.icon]::ExtractAssociatedIcon($PSHOME + "\powershell.exe")
$ADBox.Icon = $Icon

# Main menu button 2
$ADButton1 = New-Object System.Windows.Forms.Button
$ADButton1.Location = New-Object System.Drawing.Size(62,160)
$ADButton1.Size = New-Object System.Drawing.Size(500,30)
$ADButton1.Add_Click({
    $global:ButtonResult = 'Result: AD Button 1'
    $MenuBox.Close()
})
$ADButton1.Text = 'AD Button 1'
$ADButton1.Font = New-Object System.Drawing.Font("Microsoft Sans Serif",12,0,3,1)
$ADBox.Controls.Add($ADButton1)

# Main menu Header Text
$MenuHeader = New-Object System.Windows.Forms.Label
$MenuHeader.Font = New-Object System.Drawing.Font("Microsoft Sans Serif",14,1,3,1)
$MenuHeader.Location = New-Object System.Drawing.Size(118,20)
$MenuHeader.Size = New-Object System.Drawing.Size(380,40) 
$MenuHeader.Text = $Title
$MenuHeader.TextAlign = [System.Drawing.ContentAlignment]::TopCenter
$MenuBox.Controls.Add($MenuHeader)

# Main menu
$BoxLabel = New-Object System.Windows.Forms.Label
$BoxLabel.Font = New-Object System.Drawing.Font("Microsoft Sans Serif",10,0,3,1)
$BoxLabel.Location = New-Object System.Drawing.Size(10,60) 
$BoxLabel.Size = New-Object System.Drawing.Size(680,20) 
$BoxLabel.Text = 'Select the category:'
$MenuBox.Controls.Add($BoxLabel)

# Main menu button 1
$Button2 = New-Object System.Windows.Forms.Button
$Button2.Location = New-Object System.Drawing.Size(62,100)
$Button2.Size = New-Object System.Drawing.Size(500,30)
$Button2.Add_Click({
    # Call Sub menu 1
    $MenuBox.Close()
    $ADBox.Topmost = $True
    $ADBox.Add_Shown({$ADBox.Activate()})
    [void] $ADBox.ShowDialog()
})
$Button2.Text = 'Active directory'
$Button2.Font = New-Object System.Drawing.Font("Microsoft Sans Serif",12,0,3,1)
$MenuBox.Controls.Add($Button2)

# Main menu button 2
$Button1 = New-Object System.Windows.Forms.Button
$Button1.Location = New-Object System.Drawing.Size(62,160)
$Button1.Size = New-Object System.Drawing.Size(500,30)
$Button1.Add_Click({
    $global:ButtonResult = 'Result: Button 2'
    $MenuBox.Close()
})
$Button1.Text = 'Files and Folders'
$Button1.Font = New-Object System.Drawing.Font("Microsoft Sans Serif",12,0,3,1)
$MenuBox.Controls.Add($Button1)

# Exit Button
$ExitButton = New-Object System.Windows.Forms.Button
$ExitButton.Location = New-Object System.Drawing.Size(540,370)
$ExitButton.Size = New-Object System.Drawing.Size(75,23)
$ExitButton.Text = 'Exit'
$ExitButton.Font = New-Object System.Drawing.Font("Microsoft Sans Serif",11,0,3,1)
$ExitButton.Add_Click({
    Remove-Item "$WorkingDirectory\Temp\*.*" -Force
    $MenuBox.Close()
})
$MenuBox.Controls.Add($ExitButton)

# Show Menu
$MenuBox.Topmost = $True
$MenuBox.Add_Shown({$MenuBox.Activate()})
[void] $MenuBox.ShowDialog()

感谢您的帮助。

1 个答案:

答案 0 :(得分:2)

您可以在调用Close()后强制使用Dispose()方法关闭$MenuBox表单:

$Button2.Add_Click({
    # Call Sub menu 1
    $MenuBox.Close()
    $MenuBox.Dispose()
    $ADBox.Topmost = $True
    $ADBox.Add_Shown({$ADBox.Activate()})
    [void] $ADBox.ShowDialog()
})