Only trigger ItemCheck event on manual click

Ask your Windows 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.
Locked
PaschalIT
Posts: 38
Joined: Tue Apr 02, 2019 4:37 pm

Only trigger ItemCheck event on manual click

Post by PaschalIT » Mon Apr 08, 2019 12:18 pm

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
Joined: Thu Aug 18, 2011 4:56 am

Re: Only trigger ItemCheck event on manual click

Post by davidc » Mon Apr 08, 2019 12:49 pm

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: 14011
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Only trigger ItemCheck event on manual click

Post by jvierra » Mon Apr 08, 2019 1:10 pm

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
Joined: Tue Apr 02, 2019 4:37 pm

Re: Only trigger ItemCheck event on manual click

Post by PaschalIT » Tue Apr 09, 2019 11:52 am

Huh, disabling the event and reenabling it never really occurred to me for some reason. That option worked perfectly, thanks guys!

jvierra
Posts: 14011
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: Only trigger ItemCheck event on manual click

Post by jvierra » Tue Apr 09, 2019 12:33 pm

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.

Locked