Timer Tick event

Ask questions about creating Graphical User Interfaces (GUI) in PowerShell and using WinForms controls.
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 10 years and 11 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
soccsupport
Posts: 17
Last visit: Mon Dec 23, 2019 11:44 am

Timer Tick event

Post by soccsupport »

I am trying to do what I thought was simple "label" visible / not visbile (blinking) event with a timer. I have the time interval set to 1000. I triger the timer on with a checkbox. When I click the checkbox the label appears and that's all that happens. I was under the assumption the timer_tick event cylces through the code. Is this thinking wrong? :?

PowerShell Code
Double-click the code block to select all.
$timer1_Tick={
		
If ($labelWTF.Visible = "false")
 {$labelWTF.Visible = "true"}
else 
 {$labelWTF.Visible = "false"}
}
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Timer Tick event

Post by jvierra »

Try it like thsi:
PowerShell Code
Double-click the code block to select all.
$timer1_Tick={$labelWTF.Visible=(-not $labelWTF.Visible})
User avatar
soccsupport
Posts: 17
Last visit: Mon Dec 23, 2019 11:44 am

Re: Timer Tick event

Post by soccsupport »

OK at first this did not work but I was not paying attention to how I put in the block of code. So, it's working! Thank you. :D

So with that said, I don't understand the logic of the code. I want to understand why this works. What can Google to reference this for learning purposes. Once again, thank you.
PowerShell Code
Double-click the code block to select all.
$timer1_Tick={$labelWTF.Visible=(-not $labelWTF.Visible)}
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Timer Tick event

Post by jvierra »

Did you remember to start the timer?

Did you create the event with the GUI? Did the GUI create a different event name then the default?
User avatar
soccsupport
Posts: 17
Last visit: Mon Dec 23, 2019 11:44 am

Re: Timer Tick event

Post by soccsupport »

Jvierra,

See my updated post above. :mrgreen:
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Timer Tick event

Post by jvierra »

Here is a working sample. The timer is started when the form is created.

The relevant timer creation ccode is:
$timer1_Tick={
#TODO: Place custom script here
$label1.Visible=-not $label1.Visible
}


# timer1
#
$timer1.Enabled = $True
$timer1.Interval = 1000
$timer1.add_Tick($timer1_Tick)

PowerShell Code
Double-click the code block to select all.
#=============================================
# Code Generated By: SAPIEN Technologies, Inc., PowerShell Studio 2012 v3.1.17
# Generated On: 4/24/2013 10:03 AM
#========================================================================

#----------------------------------------------
# Generated Form Function
#----------------------------------------------
function Call-Demo-TImerTick_pff {

	#----------------------------------------------
	#region Import the Assemblies
	#----------------------------------------------
	[void][reflection.assembly]::Load("mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")
	[void][reflection.assembly]::Load("System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")
	[void][reflection.assembly]::Load("System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")
	[void][reflection.assembly]::Load("System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")
	[void][reflection.assembly]::Load("System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")
	[void][reflection.assembly]::Load("System.Xml, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")
	[void][reflection.assembly]::Load("System.DirectoryServices, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")
	[void][reflection.assembly]::Load("System.Core, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")
	[void][reflection.assembly]::Load("Microsoft.VisualBasic, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")
	#endregion Import Assemblies

	#----------------------------------------------
	#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
	#----------------------------------------------
	
	
	
	
	
	
	
	$FormEvent_Load={
		#TODO: Initialize Form Controls here
		
	}
	
	$timer1_Tick={
		#TODO: Place custom script here
		$label1.Visible=-not $label1.Visible
	}
	
	# --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($FormEvent_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
	#
	$form1.Controls.Add($label1)
	$form1.Controls.Add($buttonOK)
	$form1.AcceptButton = $buttonOK
	$form1.ClientSize = '284, 262'
	$form1.FormBorderStyle = 'FixedDialog'
	$form1.MaximizeBox = $False
	$form1.MinimizeBox = $False
	$form1.Name = "form1"
	$form1.StartPosition = 'CenterScreen'
	$form1.Text = "Form"
	$form1.add_Load($FormEvent_Load)
	#
	# label1
	#
	$label1.Font = "Microsoft Sans Serif, 12pt, style=Bold"
	$label1.Location = '70, 63'
	$label1.Name = "label1"
	$label1.Size = '153, 23'
	$label1.TabIndex = 1
	$label1.Text = "Timer is ticking..."
	#
	# buttonOK
	#
	$buttonOK.Anchor = 'Bottom, Right'
	$buttonOK.DialogResult = 'OK'
	$buttonOK.Location = '197, 227'
	$buttonOK.Name = "buttonOK"
	$buttonOK.Size = '75, 23'
	$buttonOK.TabIndex = 0
	$buttonOK.Text = "OK"
	$buttonOK.UseVisualStyleBackColor = $True
	#
	# timer1
	#
	$timer1.Enabled = $True
	$timer1.Interval = 1000
	$timer1.add_Tick($timer1_Tick)
	#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-Demo-TImerTick_pff | Out-Null
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Timer Tick event

Post by jvierra »

Here is the checkbox event to turn the timer on and off:
PowerShell Code
Double-click the code block to select all.
$checkboxTimerControl_CheckedChanged={
	#TODO: Place custom script here
	$timer1.Enabled=-not $timer1.enabled
}
User avatar
soccsupport
Posts: 17
Last visit: Mon Dec 23, 2019 11:44 am

Re: Timer Tick event

Post by soccsupport »

Thank you.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Timer Tick event

Post by jvierra »

Please note that, once you lern/understand how WIndows FOrms system is designed (engineered) to work you will find that the great number of events are designed to allow us to use only the very smallest amount of code to make something happen.

There are forms design tools that can do all of teh event hookups by just dragging and dropping cotrols and by selecting a transactoin or trsnformation based on a list of possible actions.

Windows Forms is designed to support tools that build forms. The Sapien Forms builders are excellent examples of the implementation of the Microsoft design used to support PowerShell's use with Windows Forms.
This topic is 10 years and 11 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked