[英]Why are my TListBox items not changing their color?
這就是我從本地服務器上的List獲取一些項目的方法。
我認為它仍然需要一些重構(很抱歉在Delphi
是這樣的初學者),但是我想更好地理解為什么ListItem
顏色沒有被改變。
我做了一些調試,發現if
條件對每種顏色工作正常,而ListItem
正在接收它,但我可能會得到錯誤的引用或使用錯誤的屬性來改變顏色。
這是完整的代碼:
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
屬性)。
要使用自定義字體顏色,必須從ListItem中排除TStyledSetting.FontColor
:
lb_item.StyledSettings:=lb_item.StyledSettings - [TStyledSetting.FontColor];
lb_item.TextSettings.FontColor := TAlphaColors.Red;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.