locked
Poweshell Form not closing RRS feed

  • Question

  • I am trying to create a Powershell form with three buttons.

    The $form.Close() isn't working

    Snippet of the code:

    [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing") 
    [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
    [void] [System.Windows.Forms.Application]::EnableVisualStyles() 
    
    $path = "C:\form\Output\text.xml"
    $file_n = "test3"
    
    #create form to confirm over-write
    $Over_form = New-Object system.Windows.Forms.Form 
    $Over_form.Text = 'Confirm'
    $Over_form.Size = New-Object System.Drawing.Size(350,200)
    $Over_form.StartPosition = 'CenterScreen'
    
    
    $Over_lbl = New-Object system.Windows.Forms.Label
    $Over_lbl.Size = New-Object System.Drawing.Size(349,100)
    $Over_lbl.Font = New-Object System.Drawing.Font("Times New Roman",18,[System.Drawing.FontStyle]::Bold)
    $Over_lbl.Text = 'NOTE:  Data exists for this system
    Overwrite, Re-Open, or Cancel?'
    
    $Over_form.Controls.Add($Over_lbl)
    
    $Ovr_Btn_Over = New-Object System.Windows.Forms.Button
    $Ovr_Btn_Over.Text = 'Overwrite'
    $Ovr_Btn_Over.Location = New-Object System.Drawing.Point(10,102)
    $Ovr_Btn_Over.Add_click({
    
    	#double-check deletion
    	$a = new-object -comobject wscript.shell 
    		$intAnswer = $a.popup("Confirm Deletion", ` 
    		0,"Delete Files",4) 
    		If ($intAnswer -eq 6) { #confirmed
    		    #delete entry in XML
    			[xml]$xml_delete = Get-Content $path
    			$xpath = "Answers/system[@name = '$file_n']"
    			
    			$delete_this = $xml_delete.SelectSingleNode($xpath)
    			
    			while ($delete_this -ne $null) {
    				$delete_this.ParentNode.RemoveChild($delete_this)
    				$delete_this = $xml_delete.SelectSingleNode($xpath)
    			}
    			
    			$xml_delete.Save($path)
    			[System.Windows.Forms.Application]::DoEvents()
    			$Over_form.Close()
    		} else { 
    			[System.Windows.Forms.Application]::DoEvents()
    		    $Over_form.Close() #This works
    		} 
    	
    	
    })
    $Over_form.Controls.Add($Ovr_Btn_Over)
    
    $Ovr_Btn_Re = New-Object System.Windows.Forms.Button
    $Ovr_Btn_Re.Text = 'Re-Open'
    $Ovr_Btn_Re.Location = New-Object System.Drawing.Point(90,102) 
    $Ovr_Btn_Re.Add_click({#Use existing data in HTML form
    	$xpath = "Answers/system[@name = '$file_n']"
    
    	[xml]$qAndA = Get-Content $path
    
    	$this_node = $qAndA.SelectSingleNode($xpath)
    
    	#snip - doing some XML stuff here
    	$Over_form.Close() #this does not work
    })
    
    $Over_form.Controls.Add($Ovr_Btn_Re)
    
    
    $Ovr_Btn_Cncl = New-Object System.Windows.Forms.Button
    $Ovr_Btn_Cncl.Text = 'Cancel'
    $Ovr_Btn_Cncl.Location = New-Object System.Drawing.Point(170,102) 
    $Ovr_Btn_Re.Add_click({
    	
    	$Over_form.Close() #This does not work
    })
    $Over_form.Controls.Add($Ovr_Btn_Cncl)
    
    
    
    
    $Over_form.ShowDialog()
    

    Why does the $Over_form.Close() not work?

    Wednesday, October 3, 2018 1:26 AM

All replies

  • Consider the following:

    Add-Type -AssemblyName System.Windows.Forms
    [System.Windows.Forms.Application]::EnableVisualStyles() 
    
    $path = "C:\form\Output\text.xml"
    $file_n = "test3"
    
    #create form to confirm over-write
    $Over_form = New-Object system.Windows.Forms.Form 
    $Over_form.Text = 'Confirm'
    $Over_form.Size = '350,200'
    $Over_form.StartPosition = 'CenterScreen'
    
    $Over_lbl = New-Object system.Windows.Forms.Label
    $Over_form.Controls.Add($Over_lbl)
    $Over_lbl.Size = '349,100'
    $Over_lbl.Font = New-Object System.Drawing.Font("Times New Roman",16,[System.Drawing.FontStyle]::Bold)
    $Over_lbl.Text = "NOTE:  Data exists for this system `nOverwrite, Re-Open, or Cancel?"
    
    $Ovr_Btn_Over = New-Object System.Windows.Forms.Button
    $Over_form.Controls.Add($Ovr_Btn_Over)
    $Ovr_Btn_Over.Text = 'Overwrite'
    $Ovr_Btn_Over.Location = '10,102'
    $Ovr_Btn_Over.Add_click({
        #double-check deletion
        $a = new-object -comobject wscript.shell 
        $intAnswer = $a.popup("Confirm Deletion",0,"Delete Files",4) 
        If ($intAnswer -eq 6) { #confirmed
            #delete entry in XML
        	[xml]$xml_delete = Get-Content $path
        	$xpath = "Answers/system[@name = '$file_n']"
        	
        	$delete_this = $xml_delete.SelectSingleNode($xpath)
        	
        	while ($delete_this -ne $null) {
        		$delete_this.ParentNode.RemoveChild($delete_this)
        		$delete_this = $xml_delete.SelectSingleNode($xpath)
        	}
        	
        	$xml_delete.Save($path)
        	[System.Windows.Forms.Application]::DoEvents()
        	$Over_form.Close()
        } else { 
        	[System.Windows.Forms.Application]::DoEvents()
            $Over_form.Close() #This works
        } 
    })
    
    $Ovr_Btn_Re = New-Object System.Windows.Forms.Button
    $Over_form.Controls.Add($Ovr_Btn_Re)
    $Ovr_Btn_Re.Text = 'Re-Open'
    $Ovr_Btn_Re.Location = '90,102'
    $Ovr_Btn_Re.Add_click({#Use existing data in HTML form
    	$xpath = "Answers/system[@name = '$file_n']"
    
    	[xml]$qAndA = Get-Content $path
    
    	$this_node = $qAndA.SelectSingleNode($xpath)
    
    	#snip - doing some XML stuff here
    	$Over_form.Close() #this does not work
    })
    
    $Ovr_Btn_Cncl = New-Object System.Windows.Forms.Button
    $Over_form.Controls.Add($Ovr_Btn_Cncl)
    $Ovr_Btn_Cncl.Text = 'Cancel'
    $Ovr_Btn_Cncl.DialogResult = 'Cancel'
    $Ovr_Btn_Cncl.Location = '170,102'
    
    $Over_form.ShowDialog()


    \_(ツ)_/



    • Edited by jrv Wednesday, October 3, 2018 1:58 AM
    Wednesday, October 3, 2018 1:56 AM
  • Cancel button is working

    THANK YOU!

    Wednesday, October 3, 2018 4:30 AM