简体   繁体   中英

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. The thing is, the script works. All connected printers to my PC are shown in the listbox and by double-clicking one, it removes. 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”. 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.

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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