Voici comment je reçois quelques éléments d'une liste située sur un serveur local.Pourquoi mes articles TListBox ne changent pas de couleur?
Je suppose qu'il faut encore un peu de refactoring (désolé d'être un débutant dans Delphi
), mais je voudrais mieux comprendre pourquoi les ListItem
couleurs ne sont pas modifiés.
J'ai fait quelques débogage et trouvé que les conditions if
fonctionnent très bien pour chaque couleur, et la ListItem
est recevoir mais je pourrais être obtenir la mauvaise référence ou en utilisant la mauvaise propriété pour changer la couleur.
Voici le code complet:
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;
peut-être votre liste est triée * * de sorte que la variable 'I' ne correspond pas à l'index de la liste plus. 'lb_gensets.ListItems [i]' fait la même chose que 'lb_gensets.ItemByIndex (i)'. Dans ce cas est plus sûr d'appliquer [cette solution] (http://stackoverflow.com/questions/33284499/how-can-i-add-a-listboxitem-programmatically/33284984#33284984) – fantaghirocco