I am using a form window to get some input from a user and I am asking in a do-while loop if the input is correct (like this:)
while(($ParID -ne 6) -or ($Research_Groups -ne 3) -or ($Customer -lt 1) -or ($Projectname -lt 1))
The problem with this request is that I wanna ask if the field is empty but if he presses cancel without entering anything the window will open again like a while(true) loop...
I have also made a cancel button to 'exit' the programm where I did put (yeah you guessed right) an exit behind this button and my problem is that PowerShell throws me an error that the programm has crashed so I am searching for a save way to stop my programm from crashing!
$objForm.Add_KeyDown({if ($_.KeyCode -eq "Escape") #Escape (ESC) -> when the key 'Escape (ESC)' has been pressed then the window closes
{$objForm.Close()}}) #windowclose -> closes the window
this it he error it throws:
DO {
$objForm = New-Object System.Windows.Forms.Form #init -> initializing window form
$objForm.Text = "Input Window v0.5" #name -> the name which will be display in the top border of the window
$objForm.Size = New-Object System.Drawing.Size(300,250) #size -> set the size of the window
$objForm.StartPosition = "CenterScreen" #location -> where the window will appear on the screen
$objForm.FormBorderStyle = 'Fixed3D' #sizing -> fixes the size of the window so you cannot make it bigger
#objForm Keys -> here are the keys 'Enter' and 'Escape (ESC)' defined to the buttons 'OK' and 'Cancel'
$objForm.KeyPreview = $True
$objForm.Add_KeyDown({if ($_.KeyCode -eq "Enter") #Enter -> when the key 'Enter' has been pressed read out the input fields and assign them to variables
{$ParID=$ParIDInbox.Text;$Research_Groups=$ResearchGroupInbox.Text;$Customer=$CustomerInbox.Text;$Projectname=$ProjectnameInbox.Text;$objForm.Close()}}) #variables -> assigns the variables
$objForm.Add_KeyDown({if ($_.KeyCode -eq "Escape") #Escape (ESC) -> when the key 'Escape (ESC)' has been pressed then the window closes
{$objFOrm.Close()}}) #windowclose -> closes the window
#OKButton -> creates a button with the value 'OK'
$OKButton = New-Object System.Windows.Forms.Button #initialization -> initializes the button
$OKButton.Location = New-Object System.Drawing.Size(75,180) #Location -> where the button is located in the window
$OKButton.Size = New-Object System.Drawing.Size(75,23) #Size -> defines the size of the button
$OKButton.Text = "OK" #value -> sets the value of the button to 'OK'
$OKButton.Add_Click({$ParID=$ParIDInbox.Text;$Research_Groups=$ResearchGroupInbox.Text;$Customer=$CustomerInbox.Text;$Projectname=$ProjectnameInbox.Text;$objForm.Close()}) #variables -> assigns the variables
$objForm.Controls.Add($OKButton) #adding -> adds the button to the window
#CancelButton -> creates a button with the value 'Cancel'
$CancelButton = New-Object System.Windows.Forms.Button #initialization -> initializes the button
$CancelButton.Location = New-Object System.Drawing.Size(150,180) #Location -> where the button is located in the window
$CancelButton.Size = New-Object System.Drawing.Size(75,23) #Size -> defines the size of the button
$CancelButton.Text = "Cancel" #value -> sets the value of the button to 'Cancel'
$CancelButton.Add_Click({$objFOrm.Close()}) #closing -> closes the window after clicked
$objForm.Controls.Add($CancelButton) #adding -> adds the button to the window
#ParID_Label -> creates a Label for the 'Par ID' input field
$ParIDLabel = New-Object System.Windows.Forms.Label #initialization -> initializes the label
$ParIDLabel.Location = New-Object System.Drawing.Size(10,10) #Location -> where the label is located in the window
$ParIDLabel.Size = New-Object System.Drawing.Size(280,20) #Size -> defines the size of the label
$ParIDLabel.Text = "Par ID (6 numbers!)" #value -> sets the value of the Label to 'Par ID (6 numbers)'
$objForm.Controls.Add($ParIDLabel) #adding -> adds the label to the window
#ParID Input Box -> Input box for the Par ID input
$ParIDInbox = New-Object System.Windows.Forms.TextBox #initialization -> initializes the input box
$ParIDInbox.Location = New-Object System.Drawing.Size(10,30) #Location -> where the label is located in the window
$ParIDInbox.Size = New-Object System.Drawing.Size(260,20) #Size -> defines the size of the inputbox
$objForm.Controls.Add($ParIDInbox) #adding -> adds the input box to the window
#Research Group Label -> creates a Label for the 'Research Group' input field
$ResearchGroupLabel = New-Object System.Windows.Forms.Label #initialization -> initializes the label
$ResearchGroupLabel.Location = New-Object System.Drawing.Size(10,50) #Location -> where the label is located in the window
$ResearchGroupLabel.Size = New-Object System.Drawing.Size(280,20) #Size -> defines the size of the label
$ResearchGroupLabel.Text = "Research Group (3 letters!)" #value -> sets the value of the Label to 'Research Group (3 letters!)'
$objForm.Controls.Add($ResearchGroupLabel) #adding -> adds the label to the window
#Research Group Input Box -> Input box for the Research Group input
$ResearchGroupInbox = New-Object System.Windows.Forms.TextBox #initialization -> initializes the input box
$ResearchGroupInbox.Location = New-Object System.Drawing.Size(10,70) #Location -> where the label is located in the window
$ResearchGroupInbox.Size = New-Object System.Drawing.Size(260,20) #Size -> defines the size of the inputbox
$objForm.Controls.Add($ResearchGroupInbox) #adding -> adds the input box to the window
#Customer Label -> creates a Label for the 'Customer' input field
$CustomerLabel = New-Object System.Windows.Forms.Label #initialization -> initializes the label
$CustomerLabel.Location = New-Object System.Drawing.Size(10,90) #Location -> where the label is located in the window
$CustomerLabel.Size = New-Object System.Drawing.Size(280,20) #Size -> defines the size of the label
$CustomerLabel.Text = "Customer" #value -> sets the value of the Label to 'Customer'
$objForm.Controls.Add($CustomerLabel) #adding -> adds the label to the window
#Customer Input Box -> Input box for the Customer input
$CustomerInbox = New-Object System.Windows.Forms.TextBox #initialization -> initializes the input box
$CustomerInbox.Location = New-Object System.Drawing.Size(10,110) #Location -> where the label is located in the window
$CustomerInbox.Size = New-Object System.Drawing.Size(260,20) #Size -> defines the size of the inputbox
$objForm.Controls.Add($CustomerInbox) #adding -> adds the input box to the window
#Projectname Label -> creates a Label for the 'Projectname' input field
$ProjectnameLabel = New-Object System.Windows.Forms.Label #initialization -> initializes the label
$ProjectnameLabel.Location = New-Object System.Drawing.Size(10,130) #Location -> where the label is located in the window
$ProjectnameLabel.Size = New-Object System.Drawing.Size(280,20) #Size -> defines the size of the label
$ProjectnameLabel.Text = "Projectname" #value -> sets the value of the Label to 'Projectname'
$objForm.Controls.Add($ProjectnameLabel) #adding -> adds the label to the window
#Projectname Input Box -> Input box for the Projectname input
$ProjectnameInbox = New-Object System.Windows.Forms.TextBox #initialization -> initializes the input box
$ProjectnameInbox.Location = New-Object System.Drawing.Size(10,150) #Location -> where the label is located in the window
$ProjectnameInbox.Size = New-Object System.Drawing.Size(260,20) #Size -> defines the size of the inputbox
$objForm.Controls.Add($ProjectnameInbox) #adding -> adds the input box to the window
$objForm.Topmost = $True #topmost -> A topmost form is a form that overlaps all the other (non-topmost!) forms!
$objForm.Add_Shown({$objForm.Activate()})
[void] $objForm.ShowDialog()
$checkInputs = {
(($using:ParID -ne 6) -or
($using:Research_Groups -ne 3) -or
($using:Customer -lt 1) -or
($using:Projectname -lt 1)) -and
!$using:Exit
}
while(Invoke-Command $checkInputs)
{
exit
}
} while($ParID)
