Why are my TListBox items not changing their color?

为君一笑 提交于 2019-12-11 10:05:17

问题


This is how I'm getting a few items from a List located on a local server.

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.

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.

Here's the full code:

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;

回答1:


By default, controls use values from current style item (see StyleLookup property).

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;


来源:https://stackoverflow.com/questions/33344468/why-are-my-tlistbox-items-not-changing-their-color

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!