Only trigger ItemCheck event on manual click

Ask your PowerShell-related questions, including questions on cmdlet development!
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 4 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
PaschalIT
Posts: 38
Last visit: Fri Jan 10, 2020 7:38 am

Only trigger ItemCheck event on manual click

Post by PaschalIT »

Hi guys, I'm back with another question! :D

This time I'm trying to figure out a way I can prevent the $checkedlistbox1_ItemCheck event from triggering when I change the checkbox status programmatically. I want the event to trigger when a user clicks to check or uncheck a box, but not when I change it within the code, since I am checking some boxes before the user gets to do any input.

Any help?

Quick edit:
I realize I could probably use the Click or MouseClick events to do this, but would there be any negative effects of doing so?
User avatar
davidc
Posts: 5913
Last visit: Mon Jul 08, 2019 8:55 am
Been upvoted: 2 times

Re: Only trigger ItemCheck event on manual click

Post by davidc »

I recommend setting a flag /variable when assigning the control and check the variable in the ItemCheck event:

Code: Select all

$form1_Load={

	#Change Check value
	$script:Loading = $true
	$checkbox1.Checked = $false
	$script:Loading = $false
}

$checkbox1_CheckedChanged={
	#TODO: Place custom script here
	if (-not $script:Loading)
	{
		...
	}
}
David
SAPIEN Technologies, Inc.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Only trigger ItemCheck event on manual click

Post by jvierra »

How to set a checkbox without causing a click event to be fired. The following is how this is usually done in C# and other languages. It is easy and requires no interim variables.

Code: Select all

$checkbox1_CheckedChanged={
	Write-Host $this.Name
}

$button1_Click={
    $checkbox1.remove_CheckedChanged($checkbox1_CheckedChanged)
    $checkbox1.Checked = -not $checkbox1.Checked
    $checkbox1.add_CheckedChanged($checkbox1_CheckedChanged)
}
Using some C# code you can actually disable events on a control or list of controls.
PaschalIT
Posts: 38
Last visit: Fri Jan 10, 2020 7:38 am

Re: Only trigger ItemCheck event on manual click

Post by PaschalIT »

Huh, disabling the event and reenabling it never really occurred to me for some reason. That option worked perfectly, thanks guys!
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Only trigger ItemCheck event on manual click

Post by jvierra »

The plus with that method is that it does not require any extra variables or changes tot he event code. This is the usual design path with forms. Events need to be lean and elf-contained. They need to work on the objects without changing or trying to maintain state. Microsoft has designed the Windows API to implement this design philosophy which actually comes from forma program and system design. It is derived from an engineering technology called a "finite-state machine" which defines the objects, actions and events and their relationships. This technology is derived from the "theory of automata". Objects must maintain their own state and not rely on external variables or actions. Actions must work on objects and not on the environment.

Global or script may seem to be easier but can lead to many unforeseen issues and also make the code harder to understand and debug.

Here is a bit of a thumbnail sketch of a backgrounder: https://en.wikipedia.org/wiki/Finite-state_machine
It is a bit formal but it should be interesting as a display of how computer programming and design are all based on well defined and documented formalities. This link underlies a great amount of the formal industry standards. A simple awareness of this is very helpful when trying to figure out how to do something. A full education in automata is not necessary. Just get a sense for how we got here in computing.
This topic is 4 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