Forum Discussion

Schulzi's avatar
Schulzi
Brass Contributor
Feb 23, 2021

Wildcards at end of Variable

 

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 -ne [System.Windows.Forms.DialogResult]::OK)

            #Select Location
            if (($Box_region.SelectedItem -eq "EU") -and ($result_region -eq [System.Windows.Forms.DialogResult]::OK))
                {
                    $form_location_EU = New-Object System.Windows.Forms.Form
                    $form_location_EU.Text = 'Locations'
                    $form_location_EU.Size = New-Object System.Drawing.Size(300,200)
                    $form_location_EU.StartPosition = 'CenterScreen'
                    $form_location_EU.TopMost = $true

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

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

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

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

                    [void] $Box_EU.Items.AddRange(@("A_Leonding_2","A_Neidling","D_Karlsruhe","D_Luckenwalde","D_Mogendorf","FR_Lyon","IT_Rovereto"))

                    $form_location_EU.Controls.Add($Box_EU)

                    do
                        {
                            $result_location_EU = $form_location_EU.ShowDialog()
                            if ($Box_EU.SelectedIndices.Count -lt 1 -and $result_location_EU -eq [System.Windows.Forms.DialogResult]::OK)
                                {
                                    [System.Windows.Forms.MessageBox]::Show("Nothing was selected, please select a location first","WARNING",0,[System.Windows.Forms.MessageBoxIcon]::Warning)
                                }
                        }until (($result_location_EU -eq [System.Windows.Forms.DialogResult]::OK -and $Box_EU.SelectedIndices.Count -ge 1) -or $result_location_EU -ne [System.Windows.Forms.DialogResult]::OK)

if ($result_location_EU -eq [System.Windows.Forms.DialogResult]::OK)
                            {
                                $Name = $textBox_Mailbox_EU.Text
                                $Email = $textBox_Mailbox_EU.Text -replace " ","."
                                $Emailwithat = "$email@rosenbauer.com"
                                $Location = $Box_EU.SelectedItems

                                $inOrder = [System.Windows.Forms.MessageBox]::Show("Location:$Location`nName:$Name`nEmail:$Emailwithat,","Everything's alright?",4,[System.Windows.Forms.MessageBoxIcon]::Question)
                                    
                            }
                        }
                    }
    }until($inOrder -eq "YES")

 

As you can see above, I'm making use of [System.Windows.Forms.Dialogresult] in my script, i got multiple of these in the script, so if you forget to choose something, you will get an error and get back to try again

 

At the end of the whole Script I want to make a dialogwindow, that asks if everything is correct, if you say NO, it will start the loop from top again BUT if you say cancel in one of the spots on another dialogwindow the whole script should get ended.

 

If I try something like this at the end:

 

until(($inOrder -eq "YES") -or ([System.Windows.Forms.DialogResult] -ne "YES"))

 

The script also will get ended if you say "NO" on the "Everything's alright?" box, because it abviously is Not equal to "yes"

 

Is there a way to repeat the script when pressing "NO" and ending it when "cancel" is pressed?

PS: 

 

until(($inOrder -eq "YES") -or ([System.Windows.Forms.DialogResult] -eq "Cancel"))

 

or

 

until(($inOrder -eq "YES") -or ([System.Windows.Forms.DialogResult] -like "Cancel"))

 

did not work for me

 

One possibility probably would be to use a wildcard at the end of a variable, like this:

$result_region
#$result_Location_
    $result_location_EU
    $result_location_LATAM
    $result_location_APAC
    $result_location_NA
#$result_type_
    $result_type_EU
    $result_type_LATAM
    $result_type_APAC
    $result_type_NA
<#
some more in the same chape...
its always like $result_something_region
#>

so in my case a variable like:

$result_type_* would be perfect, so that i can use the one, regardless how many others are there with the beginning "$result_type_"

 

Greetings
Yannik Schulz

 

No RepliesBe the first to reply

Resources