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.
This topic is 1 year and 5 months 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
Eanna_bearn
Posts: 20
Last visit: Thu Mar 09, 2023 4:36 am
Has voted: 1 time

Checkedlistbox change color when item checked

Post by Eanna_bearn »

Hello,

Product: PowerShell Studio 2022 (64 Bit)
Build: v5.8.210
OS: Windows Server 2016 Standard (64 Bit)
Build: v10.0.14393.0

I have a form with ckeckedlistbox, it contains many items generated by reading active directory attributes on users.
Can i hilight or text color or text forecolor on item that are checked ? It will add visibility to user on the form.

thanks.
User avatar
brittneyr
Site Admin
Posts: 1655
Last visit: Thu Mar 28, 2024 11:13 am
Answers: 39
Been upvoted: 30 times

Re: Checkedlistbox change color when item checked

Post by brittneyr »

[Topic moved by moderator to PowerShell GUIs forum]
Brittney
SAPIEN Technologies, Inc.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Checkedlistbox change color when item checked

Post by jvierra »

Just change the text color on the item object in the "Changed" event.

Right click the control in the toolbox to see the links to the documentation for any control including example code on the Sapien site.
Eanna_bearn
Posts: 20
Last visit: Thu Mar 09, 2023 4:36 am
Has voted: 1 time

Re: Checkedlistbox change color when item checked

Post by Eanna_bearn »

thanks for your answer. I might be dumb but i do not find how to do it :oops:

Any example to give me ?
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Checkedlistbox change color when item checked

Post by jvierra »

There is an event on any control that is triggered when the control is changed. You can find the "Changed" event for any control by right-clicking on the control in the toolbox.
Screenshot 2022-10-07 030835.png
Screenshot 2022-10-07 030835.png (31.28 KiB) Viewed 1185 times
Eanna_bearn
Posts: 20
Last visit: Thu Mar 09, 2023 4:36 am
Has voted: 1 time

Re: Checkedlistbox change color when item checked

Post by Eanna_bearn »

i found msdn help :mrgreen:

I already look into this but i don t find code example for the event i want to :

If item is checked -> text of item checked go to bold red
If item is not checked -> text return or stay default black

Really thanks for your time ;)
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Checkedlistbox change color when item checked

Post by jvierra »

Sorry. The items in a checkedlistbox cannot have different colors. YOU would have to learn how to use the primitive graphics object and the graphics command to re-write the text in a custom color.

I don't have an example of that for this control. You can search for it on the web. There are numerous examples.

I still recommend by starting with the book I posted a link to. This will also help you start to understand WinForms with PowerShell. It will also help when you search for examples.
Eanna_bearn
Posts: 20
Last visit: Thu Mar 09, 2023 4:36 am
Has voted: 1 time

Re: Checkedlistbox change color when item checked

Post by Eanna_bearn »

2022-10-07 09_30_36-90.82.228.177_8241 - Connexion Bureau à distance.png
2022-10-07 09_30_36-90.82.228.177_8241 - Connexion Bureau à distance.png (45.73 KiB) Viewed 1183 times
Here is the form
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: Checkedlistbox change color when item checked

Post by jvierra »

Here is a hint on how to do this.
  1. $listbox1_DrawItem=[System.Windows.Forms.DrawItemEventHandler]{
  2.     #Event Argument: $_ = [System.Windows.Forms.DrawItemEventArgs]
  3.    
  4.     $b = New-Object System.Drawing.SolidBrush($listbox1.Items[$_.Index].Color)
  5.     $p = New-Object System.Drawing.PointF($_.Bounds.X, $_.Bounds.Y)
  6.     $text=$listbox1.Items[$_.Index].Text
  7.     $_.Graphics.DrawString($text,$_.Font,$b,$p)
  8. }
This is for a ListBox but a CheckedList will work the same.

Attached is a demo but it is old. I wrote it a decade ago but the PS and WinForms haven't changed.
Attachments
Demo-ListboxDrawItem.psf
(22.81 KiB) Downloaded 70 times
Eanna_bearn
Posts: 20
Last visit: Thu Mar 09, 2023 4:36 am
Has voted: 1 time

Re: Checkedlistbox change color when item checked

Post by Eanna_bearn »

really really thanks !!!! :)

I discover Powershell language five month ago and i m not a dev ... for years i do some batch files, play with autoit and vbs but never learn how to code ...
i want to create a tool that help to manage accounts / groups / files / shares for multi user AD school (about 2000 users and 800 computers)

Do not laught when you see my code :D ... i try to my best but i know theres a lot of improvement to do in it :roll: . For today i do workable code but there is lots of repetitions, and i m sure there is functions i can made to simplify it.
Attachments
modificationuser.psf
(122.43 KiB) Downloaded 57 times
This topic is 1 year and 5 months 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