1

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
1
  • Well that $timer_Tick is not defined anywhere, so, its not going to be used. If you get in the habit of using Set-StrictMode in your profile, and have this sort of thing, PowerShell will let you know about it, with messages like. --- The variable '$timer1_Tick' cannot be retrieved because it has not been set. At line:40 char:22 + $timer1.add_Tick($timer1_Tick) + ~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (timer1_Tick:String) [], RuntimeException + FullyQualifiedErrorId : VariableIsUndefined Commented Jun 24, 2019 at 6:11

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

2 Comments

it does not show the GUI form, it just always return "10" when executing the script
I copy and paste from your code, it works and shows me 20 if I click on the button. @Job

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.