Page 1 of 1
ListView items set forecolor
Posted: Tue Jan 19, 2021 9:40 am
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')
Re: ListView items set forecolor
Posted: Tue Jan 19, 2021 11:36 am
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)
Re: ListView items set forecolor
Posted: Tue Jan 19, 2021 11:50 am
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.
Re: ListView items set forecolor
Posted: Tue Jan 19, 2021 4:02 pm
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.
Re: ListView items set forecolor
Posted: Mon Feb 01, 2021 3:33 am
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.