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?
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.
1
Soufiane Ar-RazoukiAuthor Commented:
Hello,

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

But as you said it's a start ;)
0
David Johnson, CD, MVPOwnerCommented:
try -like instead of -eq
1
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Soufiane Ar-RazoukiAuthor Commented:
Thank you for your answer
still same message when I click on close
0
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

1

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
0
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.