简体   繁体   English

双击列表框中的打印机删除并显示下一个 window

[英]Double Click Printer from Listbox to delete and show next window

I'm writing a powershell-gui script (is this even the right way to call them?) in which the user can choose from the listbox which printer they want to remove.我正在编写一个 powershell-gui 脚本(这甚至是调用它们的正确方法吗?),用户可以在其中从列表框中选择他们想要删除的打印机。 The thing is, the script works.问题是,脚本有效。 All connected printers to my PC are shown in the listbox and by double-clicking one, it removes.所有连接到我的 PC 的打印机都显示在列表框中,双击一个打印机,它会被删除。 The problem is: I want that, after removing a printer, the window closes and shows another second window with the label “your printer has been removed”.问题是:我希望在移除打印机后,window 关闭并显示另一个 window 和 label“您的打印机已被移除”。 Unfortunately, this doesn't work and honestly I can't find the problem.不幸的是,这不起作用,老实说我找不到问题所在。

Here the code:这里的代码:

$window = New-Object Windows.Forms.Form
$window.Text = "Choose a printer to remove"
$window.Size = New-Object Drawing.Size(440,240)
$window.StartPosition = "CenterScreen"
$window.TopMost = $true

$window2 = New-Object Windows.Forms.Form
$window2.Size = New-Object Drawing.Size(440,240)
$window2.StartPosition = "CenterScreen"
$window2.TopMost = $true

$listbox = New-Object Windows.Forms.ListBox
$listbox.Location = New-Object Drawing.Point(10,10)
$listbox.Size = New-Object Drawing.Size(370,180)

$window.Controls.Add($listbox)

Get-Printer | Sort-Object | ForEach-Object {$listBox.Items.Add($_.Name)} | Out-Null

$listbox.Add_DoubleClick({
    Remove-Printer -Name $listbox.SelectedItems[0]})

$window.ShowDialog()

Any type of help is appreciated!任何类型的帮助表示赞赏!

Why would you want to create a second form just to tell the user the selected printer is removed when you can do this quite easily with a standard mesagebox?当您可以使用标准消息框很容易地做到这一点时,您为什么要创建第二个表单来告诉用户所选打印机已被删除?

Below your code adjusted to use the messagebox that is part of the Microsoft.VisualBasic assembly.在您的代码下方调整为使用属于 Microsoft.VisualBasic 程序集的消息框。

Also, I have done that by asking if the user wants to continue removing another printer and only if the user clicks 'No' the form will close, otherwise the content of the listbox is refreshed.此外,我通过询问用户是否要继续删除另一台打印机来做到这一点,并且只有当用户单击“否”时,表单才会关闭,否则会刷新列表框的内容。

# this is for the messagebox
Add-Type -AssemblyName Microsoft.VisualBasic

$window = New-Object Windows.Forms.Form
$window.Text = "Choose a printer to remove"
$window.Size = New-Object Drawing.Size(440,240)
$window.StartPosition = "CenterScreen"
$window.TopMost = $true

$listbox = New-Object Windows.Forms.ListBox
$listbox.Location = New-Object Drawing.Point(10,10)
$listbox.Size = New-Object Drawing.Size(370,180)
Get-Printer | Sort-Object Name | ForEach-Object {[void]$listBox.Items.Add($_.Name)}
$window.Controls.Add($listbox)

#
$listbox.Add_DoubleClick({
    # remove the selected printer
    $printer = $listbox.SelectedItems[0]
    Remove-Printer -Name $printer
    # show the confirmation dialog
    $message = "Printer '$printer' has been removed.`r`nWould you like to continue?"
    # messagebox buttons can be any of "OKOnly", "OKCancel", "AbortRetryIgnore", "YesNoCancel", "YesNo", "RetryCancel"
    # messagebox icon can be any of "Critical", "Question", "Exclamation", "Information"
    # SystemModal means this is a global box that appears on top of every other form
    $answer  = [Microsoft.VisualBasic.Interaction]::MsgBox($message, "YesNo,SystemModal,Question", 'Printer removed')
    if ($answer -eq 'No') {
        # the user quits
        $window.Close()
    }
    # the user wants to continue removing printers
    # reset the listbox to show the remaining printers
    $listbox.Items.Clear()
    Get-Printer | Sort-Object Name | ForEach-Object {[void]$listBox.Items.Add($_.Name)}
})

$window.ShowDialog()
# important, destroy the window object from memory when done with it
$window.Dispose()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM