Link to home
Start Free TrialLog in
Avatar of Soufiane Ar-Razouki
Soufiane Ar-Razouki

asked on

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
Avatar of footech
footech
Flag of United States of America image

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.
Avatar of Soufiane Ar-Razouki
Soufiane Ar-Razouki

ASKER

Hello,

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

But as you said it's a start ;)
try -like instead of -eq
Thank you for your answer
still same message when I click on close
ASKER CERTIFIED SOLUTION
Avatar of footech
footech
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Yessss !!! It works fine now :) !

Thank you guys