Unhandled exeption has occured in your application. #Powershell #form #GUI #VB

Hello Scripting Guys,

First of all I created a Test GUI for powershell it works find ! the only problem is ,that
I encounter an issue with my Close button. see FUNCTION FNEXIT

can someone tell my why I receive this error message ?

#region  ScriptForm  Designer

#region  Constructor
Import-Module activedirectory

[void][System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[void][System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")

#endregion

#region  Post-Constructor  Custom  Code

#endregion

#region  Form  Creation
#Warning: It is recommended that changes inside this region be handled using the ScriptForm Designer.
#When working with the ScriptForm designer this region and any changes within may be overwritten.
#~~< Form1 >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
$Form1 = New-Object System.Windows.Forms.Form
$Form1.ClientSize = New-Object System.Drawing.Size(751, 793)
$Form1.Text = "Form1"
#~~< BtnClose >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
$BtnClose = New-Object System.Windows.Forms.Button
$BtnClose.Location = New-Object System.Drawing.Point(671, 758)
$BtnClose.Size = New-Object System.Drawing.Size(75, 23)
$BtnClose.TabIndex = 3
$BtnClose.Text = "Close"
$BtnClose.UseVisualStyleBackColor = $true
$BtnClose.add_Click({ FNexit($BtnClose) })
#~~< Textbox1 >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
$Textbox1 = New-Object System.Windows.Forms.TextBox
$Textbox1.Location = New-Object System.Drawing.Point(564, 12)
$Textbox1.Size = New-Object System.Drawing.Size(182, 20)
$Textbox1.TabIndex = 2
$Textbox1.Text = "AD"
#~~< BtnFind >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
$BtnFind = New-Object System.Windows.Forms.Button
$BtnFind.Location = New-Object System.Drawing.Point(564, 64)
$BtnFind.Size = New-Object System.Drawing.Size(182, 38)
$BtnFind.TabIndex = 1
$BtnFind.Text = "Find"
$BtnFind.UseVisualStyleBackColor = $true
$BtnFind.add_Click({ FNad($BtnFind) })

#~~< RichTextBox1 >~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
$RichTextBox1 = New-Object System.Windows.Forms.RichTextBox
$RichTextBox1.Anchor = ([System.Windows.Forms.AnchorStyles] ([System.Windows.Forms.AnchorStyles]::Bottom -bor [System.Windows.Forms.AnchorStyles]::Right ))
$RichTextBox1.Location = New-Object System.Drawing.Point(12, 12)
$RichTextBox1.RightToLeft = [System.Windows.Forms.RightToLeft]::No
$RichTextBox1.Size = New-Object System.Drawing.Size(546, 769)
$RichTextBox1.TabIndex = 0
$RichTextBox1.Text = ""
$Form1.Controls.Add($BtnClose)
$Form1.Controls.Add($Textbox1)
$Form1.Controls.Add($BtnFind)
$Form1.Controls.Add($RichTextBox1)

#endregion

#region  Custom  Code

#endregion

#region  Event  Loop

function Main
{
      [System.Windows.Forms.Application]::EnableVisualStyles()
      [System.Windows.Forms.Application]::Run($Form1)
}

#endregion

#endregion

#region  Event  Handlers
function FNad
{
      #TODO: Place custom script here
      $User = Get-Aduser $textBox1.Text -Properties *
      if ($User)
      {
            $richTextBox1.Text = $User | Out-String
      }
      else
      {
            $richTextBox1.Text = "Error in finding $($textBox1.Text)"
      }
}

function FNexit {
$response = [System.Windows.Forms.MessageBox]::Show("Do you want to close the form?", "Status", 4)
if ($response = "yes"){
            exit
      }
}


Main #This call must remain below all other event functions

#endregion
Soufiane Ar-RazoukiAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

footechCommented:
This line is wrong.
if ($response = "yes"){

Should be
if ($response -eq "yes"){

Open in new window


Not sure if that's the cause of your error, but it's a start.
Soufiane Ar-RazoukiAuthor Commented:
Hello,

Thank you for your answer !
I changed It, still same issue,

But as you said it's a start ;)
David Johnson, CD, MVPOwnerCommented:
try -like instead of -eq
Are You Protected from Q3's Internet Threats?

Every quarter, WatchGuard's Threat Lab releases a security report that analyzes the top threat trends impacting companies around the world. For Q3, we saw that 6.8% of the top 100K websites use insecure SSL protocols. Read the full report to start protecting your business today!

Soufiane Ar-RazoukiAuthor Commented:
Thank you for your answer
still same message when I click on close
footechCommented:
Instead of the exit command in the function, use
$Form1.Close()

Open in new window

Probably also want to use
$Form1.Dispose()

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Soufiane Ar-RazoukiAuthor Commented:
Yessss !!! It works fine now :) !

Thank you guys
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.