In my program, i made a function which reads a xml of a blog and put the Titles in a TListBox
. But i need to change some properties in TListBoxItem
, like font, height and color, but it doesn't change.
How to set it in runtime ?
repeat
Title := ANode.ChildNodes['title'].Text;
Link := ANode.ChildNodes['link'].Text;
Desc := ANode.ChildNodes['description'].Text;
DataPub := ANode.ChildNodes['pubDate'].Text;
SetLength(Vet_News, Pos + 1);
Vet_Nesw[Pos] := '<h2>'+Title+'</h2>'+Desc;
Itemx := TListBoxItem.Create(self);
Itemx.Text := Title;
Itemx.ItemData.Detail := DataPub;
Itemx.ItemData.accessory := TListBoxItemData.TAccessory.aMore;
Itemx.TextSettings.WordWrap := true;
Itemx.TextSettings.FontColor := TAlphaColorRec.Darkorange;
Itemx.Height := 65;
Itemx.FontColor := TAlphaColorRec.Darkorange; // i tried two ways to change the color
lbNews.AddObject(Itemx); // lbnews is a Tlistbox
Inc(Pos);
ANode := ANode.NextSibling;
until ANode = nil;
[Tested with Delphi-XE7] At runtime, Listboxitems already have a calculated style stored in :
aListboxItem.StyledSettings
. To change a setting at runtime, you first have to remove it from the list of styled settings.For example, if you want to change the
FontColor
, first remove the styled fontcolor:Then apply another one (let's say Green):
The
TStyledSetting
constants and correspondingTTextSettings
properties are listed here in Delphi's doc.Examples for changing styles at runtime can be found here and there.
Nota Bene:
theListBox.Items[i]
gives access to item content, not the item itself. To grab a ListboxItem as a control, and then act on its properties, you can use:or
Both giving exactly the same result, I cannot say if one is better.