0

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:

enter image description here

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)
0

2 Answers 2

2

Have you tried break instead of exit? This will stop your While loop. Or, if you need to exit your program entirely, you should dispose your active form $Form.Dispose() instaed of brutal exit.

EDIT:

In most cases you should not create and spawn your form itself inside do/while loop. In my opinion it's better to use while for checks not for the entire form. That is how I imagine that:

#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_OnClick = {

    if (
        ($ParIDInbox.Text.Length -lt 6) -or
        ($ResearchGroupInbox.Text.Length -lt 3) -or
        [string]::IsNullOrWhiteSpace($CustomerInbox.Text) -or
        [string]::IsNullOrWhiteSpace($ProjectnameInbox.Text.Length)
    ) {
        [System.Windows.Forms.MessageBox]::Show("Please fill out all of the form fields", "ERROR", 
            [System.Windows.Forms.MessageBoxButtons]::OK, [System.Windows.Forms.MessageBoxIcon]::Error)
    }
    else {
        $ParID=$ParIDInbox.Text
        $Research_Groups=$ResearchGroupInbox.Text
        $Customer=$CustomerInbox.Text
        $Projectname=$ProjectnameInbox.Text

        Write-Host -f Yellow "$ParID | $Research_Groups | $Customer | $Projectname"

        $objForm.Dispose()
    }
}

$OKButton.Add_Click($OKButton_OnClick) #variables -> assigns the variables
$objForm.Controls.Add($OKButton) #adding -> adds the button to the window

The code above works for me. Have just checked.

If you need to call your form multiple times just call it in a while loop - dont recreate it. Everytime you call the form it takes default values for the form objects. It's better to check form fields, not the variables you provide values to.

while ($condition) {
    $objForm.ShowDialog()
}

$objForm.Dispose()

EDIT2:

More bright ideas: you do not need to check values length - you can set the MaxLength parameter for them

$ParIDInbox.MaxLength = 6
$ResearchGroupInbox.MaxLength = 3
Sign up to request clarification or add additional context in comments.

19 Comments

So after trying: 'break' throws the same error as mentioned in the question / '$Form.Dispose()' works like '$Form.Close()' and the problem about those 2 is that if I am currently in the loop it closes them and then just opens again...
Well... looking at your code I cant imagine why there's any reason to summon form in a while loop %) And to use while inside while. Thats an absurd and a very bad practice. Seriously.
come down man ^^ That was my code from more than 1 hour ago I changed it :D
and where should I 'summon' my form instead?
To answer this I should understand why do you wrap all the form code into a Do/While loop at the first place. In most cases you need while while checking conditions. I added some code to my answer (after 'EDIT') to clarify.
|
0

You could also set a bool e.g. $Exit and check it within your while condition:

$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)
{
    # do your things here
}

7 Comments

be back in a minute I'm gonna try this out
but my problem is still that I cannot get out of my program I mean yeah I can get out but also if I don't enter anything and press 'OK'
Well, You didn't shared your whole script with us thats why I don't know which type of form you are using. But you probably want to $objForm.Close()
The problem is the script is more than 800 lines and that is a bit much and yeah I am doing $objForm.Close() already but this one throws me back to my window if there is a wrong input
okay it works fine but how do I check if OK is pressed because when there is no input and you press ok you should try it again (so the loop should start again
|

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.