Hi guys, I'm back with another question!
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?
Only trigger ItemCheck event on manual click
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.
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.
Re: Only trigger ItemCheck event on manual click
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.
SAPIEN Technologies, Inc.
Re: Only trigger ItemCheck event on manual click
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.
Using some C# code you can actually disable events on a control or list of controls.
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)
}
Re: Only trigger ItemCheck event on manual click
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
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.
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.