Forum Discussion

Schulzi's avatar
Schulzi
Brass Contributor
Feb 25, 2021

Multiple [System.Windows.Forms.DialogResult] in one Script

I have a script like this:

 

do
    {
        Add-Type -AssemblyName System.Windows.Forms
        Add-Type -AssemblyName System.Drawing

        #select region
        $form_region = New-Object System.Windows.Forms.Form
        $form_region.Text = 'Country'
        $form_region.Size = New-Object System.Drawing.Size(300,200)
        $form_region.StartPosition = 'CenterScreen'
        $form_region.TopMost = $true

        $okButton_region = New-Object System.Windows.Forms.Button
        $okButton_region.Location = New-Object System.Drawing.Point (75,120)
        $okButton_region.Size = New-Object System.Drawing.Size (75,23)
        $okButton_region.Text = "OK"
        $okButton_region.DialogResult = [System.Windows.Forms.DialogResult]::OK
        $form_region.AcceptButton = $okButton_region
        $form_region.Controls.Add($okButton_region)

        $cancelButton_region = New-Object System.Windows.Forms.Button
        $cancelButton_region.Location = New-Object System.Drawing.Point (150,120)
        $cancelButton_region.Size = New-Object System.Drawing.Size (75,23)
        $cancelButton_region.Text = "Cancel"
        $cancelButton_region.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
        $form_region.CancelButton = $cancelButton_region
        $form_region.Controls.Add($cancelButton_region)

        $label_region = New-Object System.Windows.Forms.Label
        $label_region.Location = New-Object System.Drawing.Point (10,20)
        $label_region.Size = New-Object System.Drawing.Size (280,20)
        $label_region.Text = "Please make a selection from the list below:"
        $form_region.Controls.Add($label_region)

        $Box_region = New-Object System.Windows.Forms.ListBox
        $Box_region.Location = New-Object System.Drawing.Point (10,40)
        $Box_region.Size = New-Object System.Drawing.Size (260,70)

        [void] $Box_region.Items.AddRange(@("APAC","EU","LATAM","NA"))

        $form_region.Controls.Add($Box_region)
        
        do
           {
               $result_region = $form_region.ShowDialog()
               if ($Box_region.SelectedIndices.Count -lt 1 -and $result_region -eq [System.Windows.Forms.DialogResult]::OK)
                   {
                       [System.Windows.Forms.MessageBox]::Show("Nothing was selected, please select a region first","WARNING",0,[System.Windows.Forms.MessageBoxIcon]::Warning)
                   }
           }until (($result_region -eq [System.Windows.Forms.DialogResult]::OK -and $Box_region.SelectedIndices.Count -ge 1) -or $result_region -eq [System.Windows.Forms.DialogResult]::Cancel)
$inOrder = [System.Windows.Forms.MessageBox]::Show("Location:$selected_location `n`nName:$Mailbox_Name`n`nEmail:$Mailbox_EmailAt","Everything's alright?",4,[System.Windows.Forms.MessageBoxIcon]::Question)
    }until($inOrder -eq [System.Windows.Forms.DialogResult]::Yes) -or [System.Windows.Forms.DialogResult]::Cancel

 

so what I hoped from the "until" Loop, is that if I press "Yes" in the final DialogWindow the Script gets completed or if I press "Cancel" somewhere in the loop, it just gets canceled

 

With this solution, the script gets terminated even when I press "No" at the end BUT when I press "No" , the script should just complete from top again.

 

Greetings

Yannik Schulz

 

 

No RepliesBe the first to reply

Resources