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.A ssembly]:: LoadWithPa rtialName( "System.Wi ndows.Form s")
[void][System.Reflection.A ssembly]:: LoadWithPa rtialName( "System.Dr awing")
#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.Butto n
$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.UseVisualStyleBa ckColor = $true
$BtnClose.add_Click({ FNexit($BtnClose) })
#~~< Textbox1 >~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~ ~~~~~~~~~~ ~~~~~~~~~~ ~~~~~~~~~~ ~
$Textbox1 = New-Object System.Windows.Forms.TextB ox
$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.Butto n
$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.UseVisualStyleBac kColor = $true
$BtnFind.add_Click({ FNad($BtnFind) })
#~~< RichTextBox1 >~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~ ~~~~~~~~~~ ~~~~~~~~~~ ~~~~~~~
$RichTextBox1 = New-Object System.Windows.Forms.RichT extBox
$RichTextBox1.Anchor = ([System.Windows.Forms.Anc horStyles] ([System.Windows.Forms.Anc horStyles] ::Bottom -bor [System.Windows.Forms.Anch orStyles]: :Right ))
$RichTextBox1.Location = New-Object System.Drawing.Point(12, 12)
$RichTextBox1.RightToLeft = [System.Windows.Forms.Righ tToLeft]:: No
$RichTextBox1.Size = New-Object System.Drawing.Size(546, 769)
$RichTextBox1.TabIndex = 0
$RichTextBox1.Text = ""
$Form1.Controls.Add($BtnCl ose)
$Form1.Controls.Add($Textb ox1)
$Form1.Controls.Add($BtnFi nd)
$Form1.Controls.Add($RichT extBox1)
#endregion
#region Custom Code
#endregion
#region Event Loop
function Main
{
[System.Windows.Forms.Appl ication]:: EnableVisu alStyles()
[System.Windows.Forms.Appl ication]:: 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.Mess ageBox]::S how("Do you want to close the form?", "Status", 4)
if ($response = "yes"){
exit
}
}
Main #This call must remain below all other event functions
#endregion
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.A
[void][System.Reflection.A
#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.Butto
$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.UseVisualStyleBa
$BtnClose.add_Click({ FNexit($BtnClose) })
#~~< Textbox1 >~~~~~~~~~~~~~~~~~~~~~~~~~
$Textbox1 = New-Object System.Windows.Forms.TextB
$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.Butto
$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.UseVisualStyleBac
$BtnFind.add_Click({ FNad($BtnFind) })
#~~< RichTextBox1 >~~~~~~~~~~~~~~~~~~~~~~~~~
$RichTextBox1 = New-Object System.Windows.Forms.RichT
$RichTextBox1.Anchor = ([System.Windows.Forms.Anc
$RichTextBox1.Location = New-Object System.Drawing.Point(12, 12)
$RichTextBox1.RightToLeft = [System.Windows.Forms.Righ
$RichTextBox1.Size = New-Object System.Drawing.Size(546, 769)
$RichTextBox1.TabIndex = 0
$RichTextBox1.Text = ""
$Form1.Controls.Add($BtnCl
$Form1.Controls.Add($Textb
$Form1.Controls.Add($BtnFi
$Form1.Controls.Add($RichT
#endregion
#region Custom Code
#endregion
#region Event Loop
function Main
{
[System.Windows.Forms.Appl
[System.Windows.Forms.Appl
}
#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.Mess
if ($response = "yes"){
exit
}
}
Main #This call must remain below all other event functions
#endregion
ASKER
Hello,
Thank you for your answer !
I changed It, still same issue,
But as you said it's a start ;)
Thank you for your answer !
I changed It, still same issue,
But as you said it's a start ;)
try -like instead of -eq
ASKER
Thank you for your answer
still same message when I click on close
still same message when I click on close
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Yessss !!! It works fine now :) !
Thank you guys
Thank you guys
if ($response = "yes"){
Should be
Open in new window
Not sure if that's the cause of your error, but it's a start.