I am having trouble with some PS GUI functionality.
I have tried quite a few things and have been looking everywhere (ALL THE LINKS ARE PURPLE!).
I have a cancel button on my form, right now when the user clicks the cancel button, it points to this handler, and my form will close.
$handler_cancelButton_Click={
$MainForm.Close()
}
However, once the form closes, all of the rest of my script tries to keep running (FTP, run Excel, sends emails, etc.) and throws me errors.
I have tried many different things, I have ran just the Exit
command and that closes my form, throws an error, and closes down my entire IDE (PowerGUI).
Now I have also tried things like this:
[environment]::exit(0)
[System.Windows.Forms.Application]::Exit($null)
Both, will do exactly what calling just Exit
will do. Close my form, and PowerGUI.
What I want to happen is when the user presses cancel, my form closes, and the rest of my script stops or terminates.
EDIT
Now once I have a GUI.ps1, and a Code.ps1. I guess I would call GUI.ps1 inside of Code.ps1, get the user input that I need into variables. Then take those variables and pass them from GUI.ps1, over to Code.ps1 (I need that info in order to run anything here).
Then once I have those values in Code.ps1, run everything else I am doing..?
But now if my user decided to only hit the cancel button for some reason...
(likely to not happen a lot to be honest, I more so want to get this functionality working for learning sake. They might accidentally run the script, somehow..)
...it would just close the GUI, but wouldn't the rest of Code.ps1 still run?
Or now would I be able to say something like this in my Code.ps1 script:
if($someGUIValue -eq $null -and $someOtherGUIValues - eq $null){
Exit #would I be able to run this now the way I think it would?
}
else{
#yes, run the script..
}
**FOUND ANSWER
EDIT..
What I did was I created a flag. Before any of my script had started I declared a variable like so
$terminateScript = $false
Within my event handler I have added this
$handler_cancelButton_Click={
$terminateScript = $true
$MainForm.Close()
return $terminateScript
}
Once they hit the cancel button it will set $terminateScript = $true
. So now where I am calling all of my functions I have something like this:
$username, $password, $someOtherValue, $anotherVaklue, $terminateScript = MainForm
if($terminateScript -eq $false){
SomeFunction
AnotherFunction
}
else{
Exit #this now works here, and cancels my script, without closing my IDE.
}
Thanks for the help