Welcome › Forums › General PowerShell Q&A › Run form again if conditions not met
- This topic has 2 replies, 2 voices, and was last updated 1 month, 1 week ago by
Participant.
-
AuthorPosts
-
-
December 11, 2020 at 2:04 pm #278928
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?
PowerShell123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157Add-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 filledelseif ( !$(($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 selectedelseif ( $(($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 selectedelseif ( $(($TextBoxUser).Text) -and $(($ComboBoxOldDomain).Text) -and !$(($ComboBoxNewDomain).Text) ) {[VOID][System.Windows.MessageBox]::Show("NEW DOMAIN not selected.`nTry again")[void]$Form.ShowDialog()}### no fields completedelseif ( !$(($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 luckdo {#echo "1"#[void]$Form.Refresh()echo "2"formecho "3"compareecho "4"}#while ( $(($TextBoxUser).Text) -like "cancel" -or $(($TextBoxUser).Text) -like )while ( $status -notlike "canceled" -or $status -notlike "good" )#> -
December 11, 2020 at 4:03 pm #278967
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:
PowerShell123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101Add-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() -
December 12, 2020 at 4:06 am #279030
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.
-
This reply was modified 1 month, 1 week ago by
Adrian R.
-
This reply was modified 1 month, 1 week ago by
-
-
AuthorPosts
- You must be logged in to reply to this topic.