Which event handler to use when specific checkedlistbox item is checked?

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 4 years and 1 month 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
mgiljum
Posts: 1
Last visit: Wed Dec 07, 2022 7:24 am

Which event handler to use when specific checkedlistbox item is checked?

Post by mgiljum »

I have a checked list box with static items. I need a button on my form to be enabled when a specific item in the checked list box is checked, and disabled when it's unchecked. I'm not sure which event handler to use; I'm trying ItemCheck but it's not working the way I want.

What I have so far is:

Code: Select all

$checkedlistbox_ItemCheck = [System.Windows.Forms.ItemCheckEventHandler]{
	#Event Argument: $_ = [System.Windows.Forms.ItemCheckEventArgs]
	
	if ($checkedlistbox.CheckedItems -contains "MyValue")
	{
		$button.Enabled = $true
	}
	else
	{
		$button.Enabled = $false
	}
	
}
But because the ItemCheck event handler doesn't update till after the item is checked, this doesn't work until you check another item. Is there a more elegant way to do this?
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Which event handler to use when specific checkedlistbox item is checked?

Post by jvierra »

This is how to code this1:

Code: Select all

$checkedlistbox_ItemCheck = [System.Windows.Forms.ItemCheckEventHandler]{
	#Event Argument: $_ = [System.Windows.Forms.ItemCheckEventArgs]
    if($checkedlistbox.Items[$_.Index] -eq 'MyValue'){
    	if ($_.NewValue -eq 'Checked'){
    		$button.Enabled = $true
    	}else{
    		$button.Enabled = $false
    	}
    }
}
This topic is 4 years and 1 month 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