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.
Post Reply
mgiljum
Posts: 1
Joined: Wed Jan 02, 2019 7:59 pm

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

Post by mgiljum » Sun Feb 02, 2020 7:29 pm

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

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

Post by jvierra » Sun Feb 02, 2020 7:47 pm

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
    	}
    }
}

Post Reply