为什么我的TListBox项没有改变颜色? [英] Why are my TListBox items not changing their color?
问题描述
这就是我从本地服务器上的列表中获取一些物品的方式。
This is how I'm getting a few items from a List located on a local server.
我认为它仍然需要一些重构(很抱歉, Delphi
的初学者),但是我想更好地理解为什么 ListItem
颜色是为什么
I assume it still need some refactoring (sorry for being such a beginner in Delphi
), however I would like to understand better why the ListItem
colors are not being changed.
我进行了一些调试,发现 if
条件对每种颜色都适用,并且 ListItem
正在接收,但是我可能得到了错误的引用或使用了错误的属性来更改颜色。
I did some debugging and found out that the if
conditions are working fine for each color, and the ListItem
is receiving it but I might be getting the wrong reference or using the wrong property to change the color.
这里是完整代码:
procedure TFormLogin.TimerGetListTimer(Sender: TObject);
var
genset_response: String;
genset_amount: Integer;
i: Integer;
str_array: TStringDynArray;
lb_item: TListBoxItem;
begin
// Run this timer only 1 time for now
TimerGetList.Enabled := false;
// Clear all List items
lb_gensets.Clear;
// GET_LIST command to server
IdTCPClient1.IOHandler.WriteLn('GET_LIST');
// Server returns the List in a String
genset_response := IdTCPClient1.IOHandler.ReadLn();
// Remove all " from the String
genset_response := StringReplace(genset_response, '"', '',
[rfReplaceAll, rfIgnoreCase]);
// Separate data by divider
str_array := SplitString(genset_response, '|');
// Get how many items
genset_amount := StrToInt(str_array[1]);
// Populate the List
for i := 0 to (genset_amount - 1) do
begin
if (i = 0) then
begin
lb_gensets.Items.Add(str_array[2]);
end
else
begin
// Add items
lb_gensets.Items.Add(str_array[i + 2]);
end;
// Get current ListItem
lb_item := lb_gensets.ListItems[i];
if (lb_item.Text.Contains('Online')) then
begin
// Set online items to Green color
lb_item.TextSettings.FontColor := TAlphaColors.Mediumseagreen;
end;
if (lb_item.Text.Contains('OFF LINE')) then
begin
// Set Off Line items to Red color
lb_item.TextSettings.FontColor := TAlphaColors.Red;
end;
// End of FOR
end;
end;
推荐答案
默认情况下,控件使用当前样式项中的值(参见 StyleLookup
属性。)
By default, controls use values from current style item (see StyleLookup
property).
要使用自定义字体颜色,必须排除 TStyledSetting.FontColor。
from ListItem:
For use custom font color you must exclude TStyledSetting.FontColor
from ListItem:
lb_item.StyledSettings:=lb_item.StyledSettings - [TStyledSetting.FontColor];
lb_item.TextSettings.FontColor := TAlphaColors.Red;
这篇关于为什么我的TListBox项没有改变颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!