Page 1 of 1

Only trigger ItemCheck event on manual click

Posted: Mon Apr 08, 2019 12:18 pm
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?

Re: Only trigger ItemCheck event on manual click

Posted: Mon Apr 08, 2019 12:49 pm
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)
	{
		...
	}
}

Re: Only trigger ItemCheck event on manual click

Posted: Mon Apr 08, 2019 1:10 pm
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.

Re: Only trigger ItemCheck event on manual click

Posted: Tue Apr 09, 2019 11:52 am
by PaschalIT
Huh, disabling the event and reenabling it never really occurred to me for some reason. That option worked perfectly, thanks guys!

Re: Only trigger ItemCheck event on manual click

Posted: Tue Apr 09, 2019 12:33 pm
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.