Run form again if conditions not met

Hello,

I’m trying to figure out how to solve the following situation:

I have a script with a form with 2 buttons: ok and calncel

ok > (should) return 3 values (1 input and 2 dropdownlist)

cancel > returns one value of the form with a hard coded value and end (break) the execution

 

After I complete the form, I’m running “COMPARE” function that contains multiple comparisons (if, elseif) where I compare those 3 values from OK button, with different scenarios.

ex: if one from three fields are empty, I want to rerun the form

all good for the first comparison but, if I let one field empty, the form runs again but second comparison run ends with the values obtained from the first run. From what I understand, I have to run again “run” function (loop) but I can’t figure it out how to do it right.

What I’m missing?

Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()
$Form = New-Object system.Windows.Forms.Form
$Form.ClientSize = New-Object System.Drawing.Point(336,171)
$Form.text = "Insert user to make changes"
$Form.TopMost = $false

$TextBoxUser = New-Object system.Windows.Forms.TextBox
$TextBoxUser.multiline = $false
$TextBoxUser.width = 212
$TextBoxUser.height = 20
$TextBoxUser.location = New-Object System.Drawing.Point(109,20)
$TextBoxUser.Font = New-Object System.Drawing.Font('Microsoft Sans Serif',10)

$LabelUser = New-Object system.Windows.Forms.Label
$LabelUser.text = "User"
$LabelUser.AutoSize = $true
$LabelUser.width = 212
$LabelUser.height = 20
$LabelUser.location = New-Object System.Drawing.Point(13,27)
$LabelUser.Font = New-Object System.Drawing.Font('Microsoft Sans Serif',10,[System.Drawing.FontStyle]([System.Drawing.FontStyle]::Bold))

$ComboBoxOldDomain = New-Object system.Windows.Forms.ComboBox
$ComboBoxOldDomain.width = 212
$ComboBoxOldDomain.height = 20
@('domain1','domain2') | ForEach-Object {[void] $ComboBoxOldDomain.Items.Add($_)}
$ComboBoxOldDomain.location = New-Object System.Drawing.Point(110,54)
$ComboBoxOldDomain.Font = New-Object System.Drawing.Font('Microsoft Sans Serif',10)
$ComboBoxOldDomain.DropDownStyle = [System.Windows.Forms.ComboBoxStyle]::DropDownList;

$LabelOldDomain = New-Object system.Windows.Forms.Label
$LabelOldDomain.text = "Old Domain"
$LabelOldDomain.AutoSize = $true
$LabelOldDomain.width = 212
$LabelOldDomain.height = 20
$LabelOldDomain.location = New-Object System.Drawing.Point(13,56)
$LabelOldDomain.Font = New-Object System.Drawing.Font('Microsoft Sans Serif',10,[System.Drawing.FontStyle]([System.Drawing.FontStyle]::Bold))

$LabelNewDomain = New-Object system.Windows.Forms.Label
$LabelNewDomain.text = "New Domain"
$LabelNewDomain.AutoSize = $true
$LabelNewDomain.width = 212
$LabelNewDomain.height = 20
$LabelNewDomain.location = New-Object System.Drawing.Point(13,84)
$LabelNewDomain.Font = New-Object System.Drawing.Font('Microsoft Sans Serif',10,[System.Drawing.FontStyle]([System.Drawing.FontStyle]::Bold))

$ComboBoxNewDomain = New-Object system.Windows.Forms.ComboBox
$ComboBoxNewDomain.width = 212
$ComboBoxNewDomain.height = 20
@('domain1','domain2') | ForEach-Object {[void] $ComboBoxNewDomain.Items.Add($_)}
$ComboBoxNewDomain.location = New-Object System.Drawing.Point(110,84)
$ComboBoxNewDomain.Font = New-Object System.Drawing.Font('Microsoft Sans Serif',10)
$ComboBoxNewDomain.DropDownStyle = [System.Windows.Forms.ComboBoxStyle]::DropDownList;

$Cancel = New-Object system.Windows.Forms.Button
$Cancel.text = "Cancel"
$Cancel.width = 60
$Cancel.height = 30
$Cancel.location = New-Object System.Drawing.Point(218,119)
$Cancel.Font = New-Object System.Drawing.Font('Microsoft Sans Serif',10)
$Cancel.BackColor = [System.Drawing.ColorTranslator]::FromHtml("#f5a623")

$OK = New-Object system.Windows.Forms.Button
$OK.text = "OK"
$OK.width = 60
$OK.height = 30
$OK.location = New-Object System.Drawing.Point(63,119)
$OK.Font = New-Object System.Drawing.Font('Microsoft Sans Serif',10)
$OK.BackColor = [System.Drawing.ColorTranslator]::FromHtml("#b8e986")

$Form.controls.AddRange(@($OK,$TextBoxUser,$LabelUser,$ComboBoxOldDomain,$LabelOldDomain,$Cancel,$LabelNewDomain,$ComboBoxNewDomain))

$Cancel.Add_Click({
$TextBoxUser.Text = "cancel";
$Form.Close()
})
$OK.Add_Click({ $Form.Close() })

[void]$Form.ShowDialog()

function run {
## also tryed to run the form here [void]$Form.ShowDialog()
if ( $(($TextBoxUser).Text) -like "cancel") {
$status="canceled"
echo "canceled"
break
}

elseif ( $(($TextBoxUser).Text) -and $(($ComboBoxOldDomain).Text) -and $(($ComboBoxNewDomain).Text) ) {
echo "user is $(($TextBoxUser).Text)"
echo "old domain is $(($ComboBoxOldDomain).Text)"
echo "new domain is $(($ComboBoxNewDomain).Text)"
$status="good"
### run commands that will make changes to the user
}

elseif ( $(($TextBoxUser).Text) -and $(($ComboBoxOldDomain).Text) -and $(($ComboBoxNewDomain).Text) -and $(($ComboBoxOldDomain).Text) -like $(($ComboBoxNewDomain).Text) ) {
[VOID][System.Windows.MessageBox]::Show("You selected the same domain for both OLD and NEW.`nTry again")
[void]$Form.ShowDialog()
}

#### username not filled
elseif ( !$(($TextBoxUser).Text) -and $(($ComboBoxOldDomain).Text) -and $(($ComboBoxNewDomain).Text) ) {
[VOID][System.Windows.MessageBox]::Show("USERNAME not filled.`n Try again")
[void]$Form.ShowDialog()
}

elseif ( !$(($TextBoxUser).Text) -and !$(($ComboBoxOldDomain).Text) -and $(($ComboBoxNewDomain).Text) ) {
[VOID][System.Windows.MessageBox]::Show("USERNAME not filled and OLD DOMAIN not selected.`nTry again")
[void]$Form.ShowDialog()
}

elseif ( !$(($TextBoxUser).Text) -and $(($ComboBoxOldDomain).Text) -and !$(($ComboBoxNewDomain).Text) ) {
[VOID][System.Windows.MessageBox]::Show("USERNAME not filled and NEW DOMAIN not selected.`nTry again")
[void]$Form.ShowDialog()
}

### old domain not selected
elseif ( $(($TextBoxUser).Text) -and !$(($ComboBoxOldDomain).Text) -and $(($ComboBoxNewDomain).Text) ) {
[VOID][System.Windows.MessageBox]::Show("OLD DOMAIN not selected.`nTry again")
[void]$Form.ShowDialog()
}

elseif ( $(($TextBoxUser).Text) -and !$(($ComboBoxOldDomain).Text) -and !$(($ComboBoxNewDomain).Text) ) {
[VOID][System.Windows.MessageBox]::Show("NEW DOMAIN and OLD DOMAIN not selected.`nTry again")
[void]$Form.ShowDialog()
}

### new domain not selected
elseif ( $(($TextBoxUser).Text) -and $(($ComboBoxOldDomain).Text) -and !$(($ComboBoxNewDomain).Text) ) {
[VOID][System.Windows.MessageBox]::Show("NEW DOMAIN not selected.`nTry again")
[void]$Form.ShowDialog()
}

### no fields completed
elseif ( !$(($TextBoxUser).Text) -and !$(($ComboBoxOldDomain).Text) -and !$(($ComboBoxNewDomain).Text) ) {
[VOID][System.Windows.MessageBox]::Show("Neighter of the fields were completed.`nTry again")
[void]$Form.ShowDialog()
}
}

run

<#
### also tryed this without any luck
do {
#echo "1"
#[void]$Form.Refresh()
echo "2"
form
echo "3"
compare
echo "4"
}
#while ( $(($TextBoxUser).Text) -like "cancel" -or $(($TextBoxUser).Text) -like )
while ( $status -notlike "canceled" -or $status -notlike "good" )
#>

Perform the validation when you are in the form rather than attempting to start the form again. Try submitting with no user and then put something in the user field:

Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()
$Form = New-Object system.Windows.Forms.Form
$Form.ClientSize = New-Object System.Drawing.Point(350,200)
$Form.text = "Insert user to make changes"
$Form.TopMost = $false
 
$TextBoxUser = New-Object system.Windows.Forms.TextBox
$TextBoxUser.multiline = $false
$TextBoxUser.width = 212
$TextBoxUser.height = 20
$TextBoxUser.location = New-Object System.Drawing.Point(109,20)
$TextBoxUser.Font = New-Object System.Drawing.Font('Microsoft Sans Serif',10)
 
$LabelUser = New-Object system.Windows.Forms.Label
$LabelUser.text = "User"
$LabelUser.AutoSize = $true
$LabelUser.width = 212
$LabelUser.height = 20
$LabelUser.location = New-Object System.Drawing.Point(13,27)
$LabelUser.Font = New-Object System.Drawing.Font('Microsoft Sans Serif',10,[System.Drawing.FontStyle]([System.Drawing.FontStyle]::Bold))
 
$ComboBoxOldDomain = New-Object system.Windows.Forms.ComboBox
$ComboBoxOldDomain.width = 212
$ComboBoxOldDomain.height = 20
@('domain1','domain2') | ForEach-Object {[void] $ComboBoxOldDomain.Items.Add($_)}
$ComboBoxOldDomain.location = New-Object System.Drawing.Point(110,54)
$ComboBoxOldDomain.Font = New-Object System.Drawing.Font('Microsoft Sans Serif',10)
$ComboBoxOldDomain.DropDownStyle = [System.Windows.Forms.ComboBoxStyle]::DropDownList;
 
$LabelOldDomain = New-Object system.Windows.Forms.Label
$LabelOldDomain.text = "Old Domain"
$LabelOldDomain.AutoSize = $true
$LabelOldDomain.width = 212
$LabelOldDomain.height = 20
$LabelOldDomain.location = New-Object System.Drawing.Point(13,56)
$LabelOldDomain.Font = New-Object System.Drawing.Font('Microsoft Sans Serif',10,[System.Drawing.FontStyle]([System.Drawing.FontStyle]::Bold))
 
$LabelNewDomain = New-Object system.Windows.Forms.Label
$LabelNewDomain.text = "New Domain"
$LabelNewDomain.AutoSize = $true
$LabelNewDomain.width = 212
$LabelNewDomain.height = 20
$LabelNewDomain.location = New-Object System.Drawing.Point(13,84)
$LabelNewDomain.Font = New-Object System.Drawing.Font('Microsoft Sans Serif',10,[System.Drawing.FontStyle]([System.Drawing.FontStyle]::Bold))

$LabelMessage = New-Object system.Windows.Forms.Label
#$LabelMessage.text = "testing"
$LabelMessage.AutoSize = $true
$LabelMessage.width = 212
$LabelMessage.height = 20
$LabelMessage.location = New-Object System.Drawing.Point(13,120)
$LabelMessage.Font = New-Object System.Drawing.Font('Microsoft Sans Serif',10,[System.Drawing.FontStyle]([System.Drawing.FontStyle]::Bold))
$LabelMessage.ForeColor = 'Red'
 
$ComboBoxNewDomain = New-Object system.Windows.Forms.ComboBox
$ComboBoxNewDomain.width = 212
$ComboBoxNewDomain.height = 20
@('domain1','domain2') | ForEach-Object {[void] $ComboBoxNewDomain.Items.Add($_)}
$ComboBoxNewDomain.location = New-Object System.Drawing.Point(110,84)
$ComboBoxNewDomain.Font = New-Object System.Drawing.Font('Microsoft Sans Serif',10)
$ComboBoxNewDomain.DropDownStyle = [System.Windows.Forms.ComboBoxStyle]::DropDownList;
 
$Cancel = New-Object system.Windows.Forms.Button
$Cancel.text = "Cancel"
$Cancel.width = 60
$Cancel.height = 30
$Cancel.location = New-Object System.Drawing.Point(218,147)
$Cancel.Font = New-Object System.Drawing.Font('Microsoft Sans Serif',10)
$Cancel.BackColor = [System.Drawing.ColorTranslator]::FromHtml("#f5a623")
 
$OK = New-Object system.Windows.Forms.Button
$OK.text = "OK"
$OK.width = 60
$OK.height = 30
$OK.location = New-Object System.Drawing.Point(63,147)
$OK.Font = New-Object System.Drawing.Font('Microsoft Sans Serif',10)
$OK.BackColor = [System.Drawing.ColorTranslator]::FromHtml("#b8e986")
 
$Form.controls.AddRange(@($OK,$TextBoxUser,$LabelUser,$ComboBoxOldDomain,$LabelOldDomain,$Cancel,$LabelNewDomain,$ComboBoxNewDomain, $LabelMessage))
 
$Cancel.Add_Click({
#$TextBoxUser.Text = "cancel";
    $Form.Close()
})

$OK.Add_Click({ 
    if ([string]::IsNullOrEmpty($TextBoxUser.Text)) {
        $LabelMessage.Text = 'Provide a valid User'
    }
    else {
        $LabelMessage.ForeColor = 'Green'
        $LabelMessage.Text = 'Thank you'
        Start-Sleep -Seconds 5
        $Form.Close()
    }

    
 })
 
[void]$Form.ShowDialog()

Thanks for your reply.

I also tried something similar. Unfortunately, no luck on returning the values and/or run other commands/functions.

 

If everything is ok (else in your example), I need to run one or more functions that are using those 3 values. Unfortunately, I can’t make it call the function, not even an echo after I press “ok” button.