ListView items set forecolor

Ask your PowerShell-related questions, including questions on cmdlet development!
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 3 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
crunchingnumbers
Posts: 3
Last visit: Thu Nov 09, 2023 9:50 pm

ListView items set forecolor

Post by crunchingnumbers »

According to Sapien guidance this should work

[ListViewSubItem] Add ([String] text, [Color] foreColor, [Color] backColor, [Font] font)

I can add a string no problem but cannot get foreColor to work - does anyone have an example - Spaien only lists this:
$subItem = $listitem.SubItems.Add('Installed')
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: ListView items set forecolor

Post by jvierra »

If I remember correctly you will have to create ne3w2 color objects and font objects so the objects are references and not static objects. The API ignores these if they are of the wrong type.

$c = [system.drawing.color]::FromKnownColor('Black')

Here is another way to add colored items:

Code: Select all

	$items=@(
		[PSCustomObject]@{ Text = 'This is red';Color='Red'}
		[PSCustomObject]@{ Text = 'This is blue'; Color = 'Blue' }
		[PSCustomObject]@{ Text = 'This is green'; Color = 'Green' }
	)
	$listbox1.Items.AddRange($items)
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: ListView items set forecolor

Post by jvierra »

Also note that this works:

$lvsi = [System.Windows.Forms.ListViewItem+ListViewSubItem]::new($lvi,'hello world','Red','black','TImes')

Your example is missing the owner which is required on subitems. ListviewSubItem does not have an "Add" method so I suspect you have other mistakes in your code.

The following also works:

$lvsi = $lvi.SubItems.Add('Hello World','Red','Black','Times')

Where $lvi is a ListViewItem.
crunchingnumbers
Posts: 3
Last visit: Thu Nov 09, 2023 9:50 pm

Re: ListView items set forecolor

Post by crunchingnumbers »

jvierra

Thank you very much - i must have missed something when applying this to the subitem although the string worked which was the puzzling part. I'll look more closely at what i coded and the example you gave. Thanks again.
DanielJohnson
Posts: 6
Last visit: Tue Feb 02, 2021 11:54 pm

Re: ListView items set forecolor

Post by DanielJohnson »

jvierra wrote: Tue Jan 19, 2021 11:36 am
Here is another way to add colored items:

Code: Select all

	$items=@(
		[PSCustomObject]@{ Text = 'This is red';Color='Red'} 
		[PSCustomObject]@{ Text = 'This is blue'; Color = 'Blue' }
		[PSCustomObject]@{ Text = 'This is green'; Color = 'Green' }
	)
	$listbox1.Items.AddRange($items)
Thanks! You helped me a lot.
This topic is 3 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