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 4 weeks and 2 days old and the forum Administrator has chosen for old topics to be locked when a reply is attempted. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
crunchingnumbers
Posts: 3
Joined: Sun Jan 10, 2021 7:49 am

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: 14696
Joined: Tue May 22, 2007 9:57 am
Answers: 8
Has voted: 2 times
Been upvoted: 5 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: 14696
Joined: Tue May 22, 2007 9:57 am
Answers: 8
Has voted: 2 times
Been upvoted: 5 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
Joined: Sun Jan 10, 2021 7:49 am

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
Joined: Mon Feb 01, 2021 3:23 am

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 4 weeks and 2 days old and the forum Administrator has chosen for old topics to be locked when a reply is attempted. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked