Checkedlistbox change color when item 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.
jvierra
Posts: 15337
Last visit: Wed Nov 30, 2022 12:48 am
Answers: 27
Has voted: 4 times
Been upvoted: 30 times

Re: Checkedlistbox change color when item checked

Post by jvierra »

I don't laugh at anyone's code, but I will try to give simple pointers because you have no programming experience. That is to be expected. Most people with programming experience in Windows ask questions in developer forums. This forum is for anyone who uses PS and WinForms.

You can use PS as a batch language. PS was designed to do that to support basic user needs. WinForms is very technical but learning PS to a deeper level will always help with Forms.

Time and experience and reading the docs and books on PowerShell will get you moving along quite efficiently.

Good luck. You have a long and fun journey ahead of you.

Also, you might want to search for articles on forms design. That can save you a lot of wasted time and reduce the amount of code you write.
Eanna_bearn
Posts: 11
Last visit: Tue Oct 25, 2022 12:11 am
Has voted: 1 time

Re: Checkedlistbox change color when item checked

Post by Eanna_bearn »

do i integrate your code like that ?

Code: Select all

$checkedlistboxGroupsoption_SelectedIndexChanged={
	#TODO: Place custom script here
	$checkedlistboxGroupsoption_DrawItem = [System.Windows.Forms.DrawItemEventHandler]{
		#Event Argument: $_ = [System.Windows.Forms.DrawItemEventArgs]
		
		$b = New-Object System.Drawing.SolidBrush($checkedlistboxGroupsoption.Items[$_.Index].Color)
		$p = New-Object System.Drawing.PointF($_.Bounds.X, $_.Bounds.Y)
		$text = $checkedlistboxGroupsoption.Items[$_.Index].Text
		$_.Graphics.DrawString($text, $_.Font, $b, $p)
	}
}
jvierra
Posts: 15337
Last visit: Wed Nov 30, 2022 12:48 am
Answers: 27
Has voted: 4 times
Been upvoted: 30 times

Re: Checkedlistbox change color when item checked

Post by jvierra »

You cannot just arbitrarily copy and insert code without understanding the events and how WinForms work. You really need to learn PowerShell and WinForms.

No. What you are doing won't work. The form I posted will give you an idea, but you will have to try to understand why it is written the way it is. For that you have to understand "events" and how Windows dispatches events and how the code interacts with the event. Every event has certain things available that other events don't have.
Locked