Search code examples
powershellerrorlevel

How do I return errorlevel by handling GUI using PowerShell?


I have a GUI, the form will close automatically in 10s. But if we click the pause button, it also will close the form. I want to return the error level each process that I do. If the form closes automatically, it will return error level 10. but if we click the button pause, it will return error level 20. anyone can help, please.

this is my code.

    function Timer_GUI {
    [System.Windows.Forms.Application]::EnableVisualStyles()
    $form1 = New-Object 'System.Windows.Forms.Form'
    $label1 = New-Object 'System.Windows.Forms.Label'
    $label2 = New-Object 'System.Windows.Forms.Label'
    $Cancel = New-Object 'System.Windows.Forms.Button'
    $timer1 = New-Object 'System.Windows.Forms.Timer'

    $form1_Load = { 
        $TotalTime = 10 #in seconds
            $script:StartTime = (Get-Date).AddSeconds($TotalTime)
            #Start the timer
            $timer1.Start()
    }
    $label1.Location = New-Object System.Drawing.Size(220,60)
    $label1.Size = New-Object System.Drawing.Size(500,30)

    $label2.Text = "The Process Will Continue in 10s"
    $label2.Location = New-Object System.Drawing.Size(140,30)
    $label2.Size = New-Object System.Drawing.Size(500,30)

    $form1.SuspendLayout()
    $form1.Controls.Add($label1)
    $form1.Controls.Add($label2)
    $form1.Controls.Add($Cancel)
    $form1.Width = 500
    $form1.Height = 200
    $form1.StartPosition = "CenterScreen"
    $form1.BackColor = "#e2e2e2"
    $form1.add_Load($form1_Load)

    $Cancel.DialogResult = 'Cancel'
    $Cancel.Location = New-Object System.Drawing.Size(350,100)
    $Cancel.Size = New-Object System.Drawing.Size(100,30)
    $Cancel.Text = "Pause"
    $Cancel.add_Click($Cancel_Click)
    $timer1.add_Tick($timer1_Tick)
    $form1.ResumeLayout()


    #Show the Form
    return $form1.ShowDialog()
    exit 100
}
#Call the form
Timer_GUI | Out-Null

Solution

  • Not a GUI expert, but, here it is. Add a global variable $Global:formresult default set to 10, if button clicked set to 20.

    The following 3 lines added or updated,

    Add-Type -AssemblyName System.Windows.Forms
    $Global:formresult = 10
    $Cancel.add_Click({ $Global:formresult = 20 })
    $Global:formresult
    

    enter image description here

    Full code, copy and paste from yours.

    Add-Type -AssemblyName System.Windows.Forms
    function Timer_GUI {
        [System.Windows.Forms.Application]::EnableVisualStyles()
        $form1 = New-Object 'System.Windows.Forms.Form'
        $label1 = New-Object 'System.Windows.Forms.Label'
        $label2 = New-Object 'System.Windows.Forms.Label'
        $Cancel = New-Object 'System.Windows.Forms.Button'
        $timer1 = New-Object 'System.Windows.Forms.Timer'
        $Global:formresult = 10
    
        $form1_Load = { 
            $TotalTime = 10 #in seconds
                $script:StartTime = (Get-Date).AddSeconds($TotalTime)
                #Start the timer
                $timer1.Start()
        }
        $label1.Location = New-Object System.Drawing.Size(220,60)
        $label1.Size = New-Object System.Drawing.Size(500,30)
    
        $label2.Text = "The Process Will Continue in 10s"
        $label2.Location = New-Object System.Drawing.Size(140,30)
        $label2.Size = New-Object System.Drawing.Size(500,30)
    
        $form1.SuspendLayout()
        $form1.Controls.Add($label1)
        $form1.Controls.Add($label2)
        $form1.Controls.Add($Cancel)
        $form1.Width = 500
        $form1.Height = 200
        $form1.StartPosition = "CenterScreen"
        $form1.BackColor = "#e2e2e2"
        $form1.add_Load($form1_Load)
    
        $Cancel.DialogResult = 'Cancel'
        $Cancel.Location = New-Object System.Drawing.Size(350,100)
        $Cancel.Size = New-Object System.Drawing.Size(100,30)
        $Cancel.Text = "Pause"
        $Cancel.add_Click({ $Global:formresult = 20 })
        $timer1.add_Tick($timer1_Tick)
        $form1.ResumeLayout()
    
    
        #Show the Form
        return $form1.ShowDialog()
        exit 100
    }
    #Call the form
    Timer_GUI | Out-Null
    
    $Global:formresult