Author Posts

July 8, 2016 at 12:28 pm

I'm writing a powershell form. I want to close the form once a condition is met, without user interruption.
$objform.showdialog() not supporting $objform.close().. ($objform.show() supports $objform.close() but this is making my system hang)

Code is as below.. Any suggestions..

The below code works as it uses $objform.show() and $objform.close() after a while loop. But System hangs until condition is met

[void][System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
[void][System.Reflection.Assembly]::LoadWithPartialName("system.windows.forms")

$objform = New-Object System.Windows.Forms.Form
$objform.Text = " Team Notification"
$objform.Size = New-Object System.Drawing.size(500,100)
$objform.StartPosition = "centerScreen"
$objform.BackColor = "#0065BD"
$objform.ForeColor = "white"
$objfont = New-Object System.Drawing.Font("Times New Roman",14,[System.Drawing.FontStyle]::Italic)
$objform.font = $objFont
$objl = New-Object System.Windows.Forms.Label
$objl.Location = New-Object System.Drawing.Size(10,20)
$objl.size = New-Object System.Drawing.Size(500,100)
$objl.text = "test"
$objform.Controls.add($objl)
$objform.TopMost = $true
$objform.add_shown({$objform.Activate()})
$objform.show()
$val = $true
while($val -ne $false)
{
$val = Test-Path C:\Windows\Temp\test.txt
$i++}
$objform.close()

  • This topic was modified 2 years, 1 month ago by  Shesha A.V.

July 8, 2016 at 12:31 pm

It's possible that loop is just sticking the event queue. You might try adding a Start-Sleep In there for a second, so the system can catch up.

July 8, 2016 at 12:37 pm

You aren't going to be able to do anything with this form. While the condition is true you'll never break out of the loop.

Options.

Use a timer and jobs to evaluate the condition every few seconds or so.
Use the filesystem watcher class to monitor changes, pop up a message box to say that the txt file has been deleted or something. Close the form after the user clicks ok.

July 8, 2016 at 12:40 pm

That's Gr8!!! it seems to be working!! I will try in my actual script and will post if i face any issue...

Thanks and Regards,
Shesha

July 8, 2016 at 12:48 pm

This is just a test script to reproduce the error. My actual script is something different. Writing a script to uninstall office 2010, 2013, 2016 volume license products and install the O365 click to run. I am trying to add user information pop-ups so that the process should not get disturbed..

July 8, 2016 at 1:01 pm

Here you go, start it up and delete test.txt.



#----------------------------------------------
# Generated Form Function
#----------------------------------------------
function Call-forums_psf {

	#----------------------------------------------
	#region Import the Assemblies
	#----------------------------------------------
	[void][reflection.assembly]::Load('System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089')
	
	#endregion Import Assemblies

	#----------------------------------------------
	#region Generated Form Objects
	#----------------------------------------------
	[System.Windows.Forms.Application]::EnableVisualStyles()
	$form1 = New-Object 'System.Windows.Forms.Form'
	$textbox1 = New-Object 'System.Windows.Forms.TextBox'
	$filesystemwatcher1 = New-Object 'System.IO.FileSystemWatcher'
	$InitialFormWindowState = New-Object 'System.Windows.Forms.FormWindowState'
	#endregion Generated Form Objects

	#----------------------------------------------
	# User Generated Script
	#----------------------------------------------
	
	$form1_Load={
		
	}
	
	
	
	$filesystemwatcher1_Changed=[System.IO.FileSystemEventHandler]{
	#Event Argument: $_ = [System.IO.FileSystemEventArgs]
		
	}
	
	$filesystemwatcher1_Deleted=[System.IO.FileSystemEventHandler]{
	#Event Argument: $_ = [System.IO.FileSystemEventArgs]
		$textbox1.Text = 'C:\Windows\Temp\test.txt deleted'
	}
	
	$filesystemwatcher1_Created=[System.IO.FileSystemEventHandler]{
	#Event Argument: $_ = [System.IO.FileSystemEventArgs]
		$textbox1.Text = 'C:\Windows\Temp\test.txt created'
	}
	
	# --End User Generated Script--
	#----------------------------------------------
	#region Generated Events
	#----------------------------------------------
	
	$Form_StateCorrection_Load=
	{
		#Correct the initial state of the form to prevent the .Net maximized form issue
		$form1.WindowState = $InitialFormWindowState
	}
	
	$Form_Cleanup_FormClosed=
	{
		#Remove all event handlers from the controls
		try
		{
			$form1.remove_Load($form1_Load)
			$filesystemwatcher1.remove_Changed($filesystemwatcher1_Changed)
			$filesystemwatcher1.remove_Created($filesystemwatcher1_Created)
			$filesystemwatcher1.remove_Deleted($filesystemwatcher1_Deleted)
			$form1.remove_Load($Form_StateCorrection_Load)
			$form1.remove_FormClosed($Form_Cleanup_FormClosed)
		}
		catch [Exception]
		{ }
	}
	#endregion Generated Events

	#----------------------------------------------
	#region Generated Form Code
	#----------------------------------------------
	$form1.SuspendLayout()
	$filesystemwatcher1.BeginInit()
	#
	# form1
	#
	$form1.Controls.Add($textbox1)
	$form1.ClientSize = '363, 126'
	$form1.Name = 'form1'
	$form1.Text = 'Form'
	$form1.add_Load($form1_Load)
	#
	# textbox1
	#
	$textbox1.Location = '30, 59'
	$textbox1.Name = 'textbox1'
	$textbox1.Size = '266, 20'
	$textbox1.TabIndex = 0
	#
	# filesystemwatcher1
	#
	$filesystemwatcher1.EnableRaisingEvents = $True
	$filesystemwatcher1.Filter = 'test.txt'
	$filesystemwatcher1.Path = 'C:\Windows\Temp'
	$filesystemwatcher1.SynchronizingObject = $form1
	$filesystemwatcher1.add_Changed($filesystemwatcher1_Changed)
	$filesystemwatcher1.add_Created($filesystemwatcher1_Created)
	$filesystemwatcher1.add_Deleted($filesystemwatcher1_Deleted)
	$filesystemwatcher1.EndInit()
	$form1.ResumeLayout()
	#endregion Generated Form Code

	#----------------------------------------------

	#Save the initial state of the form
	$InitialFormWindowState = $form1.WindowState
	#Init the OnLoad event to correct the initial state of the form
	$form1.add_Load($Form_StateCorrection_Load)
	#Clean up the control events
	$form1.add_FormClosed($Form_Cleanup_FormClosed)
	#Show the Form
	return $form1.ShowDialog()

} #End Function

#Call the form
Call-forums_psf | Out-Null

July 15, 2016 at 10:29 am

Is it possible to autoclose a form which uses $objform.showdialog() ? I am not able to do so. I am trying to Auto close a window once the timer is empty and execute an installation..
here is my code.. Is there anyway i can autoclose the below once the timer becomes '0'. Below is the script..

i tried
$okbutton.add_click($okbutton.click)
$okbutton.click{$objform.close()}

but didn't work

$HorizontalRsltn = (Get-WmiObject -Class win32_videocontroller).currenthorizontalResolution

$VerticalRsltn = (Get-WmiObject -Class win32_videocontroller).currentverticalResolution

$Formlocwidth = $HorizontalRsltn * 0.60

$Formlocheight = $VerticalRsltn * 0.65

$formwidth = $HorizontalRsltn * 0.40

$formheight = $VerticalRsltn * 0.30

$Okbtnwidth = $formwidth * 0.35

$Okbtnheight = $formheight * 0.65

$labelendwidth = $formwidth *0.98

$labelendheight = $formheight

 

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

[void][System.Reflection.Assembly]::LoadWithPartialName("system.windows.forms")

$objform = New-Object System.Windows.Forms.Form

$objform.Text = "Team Notification"

$objform.Size = New-Object System.Drawing.size($formwidth,$formheight)

$objform.StartPosition = "Manual"

$objform.Location = New-Object System.Drawing.size($Formlocwidth,$Formlocheight)

$objform.BackColor = "#0065BD"

$objform.ForeColor = "white"

$objfont = New-Object System.Drawing.Font("Times New Roman",12,[System.Drawing.FontStyle]::Italic)

$objform.font = $objFont

$objform.KeyPreview = $true

$OKButton = New-Object System.Windows.Forms.Button

$OKButton.Location = New-Object System.Drawing.Size($Okbtnwidth ,$Okbtnheight)

#$OKButton.Size = New-Object System.Drawing.Size(100,30)

$OKButton.text = "OK"

$OKButton.add_click({$objform.close()})

$objform.Controls.Add($OKButton)

$objl = New-Object System.Windows.Forms.Label

$objl.Location = New-Object System.Drawing.Size(2,2)

$objl.size = New-Object System.Drawing.Size($labelendwidth,$labelendheight)

$objl.text = "A critical update for MS Office is scheduled to be installed in another 1 hour.If the installation can begin now, please Save and Close all office related application And Click 'OK'. Else office applications will get closed automatically and installation will Proceed $text"

$objform.Controls.add($objl)

$objform.TopMost = $True

$objform.add_shown({$objform.Activate()})

$x = 21

[powershell]$command = [powershell]::Create().AddScript({

param

(
[System.Windows.Forms.Label]$label,
$x
)

While($x -gt 0){
$min = [int](([string]($x/60)).split('.')[0])
$text = "A critical update for MS Office is scheduled to be installed in another 30 Minutes .If the installation can begin now, please Save and Close all office related application And Click 'OK'. Else office applications will get closed automatically and installation will Proceed`n" + " " +$min + " Minutes " + ($x %60) + "seconds left"
$label.BeginInvoke([system.action[string]]{
param($text)
$label.Text = $text}, $text)
start-sleep -Seconds 1
$x–
}
})

$command.AddParameters(@($objl, $x)) | out-null
$command.BeginInvoke() | Out-Null
$objform.showdialog()
start-sleep 2