'Trying to make some boxes with form

I am trying to make a box with form that asks for a password, if the password is correct it will print access granted, and if it is wrong you can press retry to try again. i managed to do all this, but i can't get the retry button to go back to the first window. please help :)

i have searched a lot for form details and tried using $button.add_click, but i can't seem to make that work either

code:

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

$form = New-Object System.Windows.Forms.Form
$form.Text = 'Secret Terminal'
$form.Size = New-Object System.Drawing.Size(300,200)
$form.StartPosition = 'CenterScreen'

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

$label = New-Object System.Windows.Forms.Label
$label.Location = New-Object System.Drawing.Point(10,20)
$label.Size = New-Object System.Drawing.Size(280,20)
$label.Text = 'Please enter the secret passcode:'
$form.Controls.Add($label)

$textBox = New-Object System.Windows.Forms.TextBox
$textBox.Location = New-Object System.Drawing.Point(10,40)
$textBox.Size = New-Object System.Drawing.Size(260,20)
$form.Controls.Add($textBox)

$form.Topmost = $true

$form.Add_Shown({$textBox.Select()})
$result = $form.ShowDialog()

if ($result -eq [System.Windows.Forms.DialogResult]::OK)
{
    $x = $textBox.Text
}

if ($x -eq "password") {
$form2 = New-Object System.Windows.Forms.Form
$form2.Text = 'Secret Terminal'
$form2.Size = New-Object System.Drawing.Size(300,200)
$form2.StartPosition = 'CenterScreen'

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

$label2 = New-Object System.Windows.Forms.Label
$label2.Location = New-Object System.Drawing.Point(100,50)
$label2.Size = New-Object System.Drawing.Size(280,20)
$label2.Text = 'Access Granted'
$form2.Controls.Add($label2)

$form2.Topmost = $true

$result2 = $form2.ShowDialog()
}
else {
$form3 = New-Object System.Windows.Forms.Form
$form3.Text = 'Secret Terminal'
$form3.Size = New-Object System.Drawing.Size(300,200)
$form3.StartPosition = 'CenterScreen'

$okButton3 = New-Object System.Windows.Forms.Button
$okButton3.Location = New-Object System.Drawing.Point(105,120)
$okButton3.Size = New-Object System.Drawing.Size(75,23)
$okButton3.Text = 'RETRY'
$okButton3.DialogResult = [System.Windows.Forms.DialogResult]::OK
$form3.AcceptButton = $okButton3
$form3.Controls.Add($okButton3)

$label3 = New-Object System.Windows.Forms.Label
$label3.Location = New-Object System.Drawing.Point(100,50)
$label3.Size = New-Object System.Drawing.Size(280,20)
$label3.Text = 'Access Denied'
$form3.Controls.Add($label3)

$form3.Topmost = $true

$result3 = $form3.ShowDialog()
}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source