How to Auto close $form.Showdialog()

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
$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

$button.DialogResult = ‘ok’

Here’s a good article on the subject.

Thanks for the reply Dan,
But $okbutton.dialogresult =“OK” needs a user action. I want to close the form once timer is empty. I searched a lot. But what i understood is, powershell cannot close the $objform.showdialog() with $objform.close() automatically as it is using a single thread.

Let me know if anyone has solution for this pls…

Thanks and Regards,
Shesh

First, use the right control for the job. Powershell studio will make your life a whole lot easier.



#endregion Application Functions

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

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

	#----------------------------------------------
	#region Generated Form Objects
	#----------------------------------------------
	[System.Windows.Forms.Application]::EnableVisualStyles()
	$form1 = New-Object 'System.Windows.Forms.Form'
	$label1 = New-Object 'System.Windows.Forms.Label'
	$buttonOK = New-Object 'System.Windows.Forms.Button'
	$timer1 = New-Object 'System.Windows.Forms.Timer'
	$InitialFormWindowState = New-Object 'System.Windows.Forms.FormWindowState'
	#endregion Generated Form Objects

	#----------------------------------------------
	# User Generated Script
	#----------------------------------------------
	
	
	
	$form1_Load = {
		
		$TotalTime = 10 #in seconds
		
			$script:StartTime = (Get-Date).AddSeconds($TotalTime)
			#Start the timer
			$timer1.Start()
		
		
	}
	
	
	
	$buttonOK_Click={
		
	
		
	}
	
	
	$timer1_Tick={
			#Use Get-Date for Time Accuracy
			[TimeSpan]$span = $script:StartTime - (Get-Date)
			
			#Update the display
			$form1.Text = $label1.Text = "{0:N0}" -f $span.TotalSeconds
			
			if ($span.TotalSeconds -le 0) {
				$timer1.Stop()
				$form1.Close()
			}
		}
	
	
	# --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
		{
			$buttonOK.remove_Click($buttonOK_Click)
			$form1.remove_Load($form1_Load)
			$timer1.remove_Tick($timer1_Tick)
			$form1.remove_Load($Form_StateCorrection_Load)
			$form1.remove_FormClosed($Form_Cleanup_FormClosed)
		}
		catch [Exception]
		{ }
	}
	#endregion Generated Events

	#----------------------------------------------
	#region Generated Form Code
	#----------------------------------------------
	$form1.SuspendLayout()
	#
	# form1
	#
	$form1.Controls.Add($label1)
	$form1.Controls.Add($buttonOK)
	$form1.ClientSize = '284, 262'
	$form1.Name = 'form1'
	$form1.Text = 'Form'
	$form1.add_Load($form1_Load)
	#
	# label1
	#
	$label1.Font = 'Microsoft Sans Serif, 20.25pt'
	$label1.Location = '61, 65'
	$label1.Name = 'label1'
	$label1.Size = '173, 62'
	$label1.TabIndex = 2
	$label1.Text = 'label1'
	#
	# buttonOK
	#
	$buttonOK.DialogResult = 'OK'
	$buttonOK.Location = '142, 175'
	$buttonOK.Name = 'buttonOK'
	$buttonOK.Size = '75, 23'
	$buttonOK.TabIndex = 1
	$buttonOK.Text = 'OK'
	$buttonOK.UseVisualStyleBackColor = $True
	$buttonOK.add_Click($buttonOK_Click)
	#
	# timer1
	#
	$timer1.add_Tick($timer1_Tick)
	$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-dialogue_psf | Out-Null

Dan Thank you so much!! It worked like a charm. Thanks again. I will be cautious while using right control going forward…

Dan / Shesha is it possible to add a line of text above the timer display?
I have attempted to but I am still learning powershell.

Yes, you would use a label control. https://msdn.microsoft.com/en-us/library/System.Windows.Forms.Label.aspx