Why is my TListBox getting blank when its last TListBoxItem is checked?

125 Views Asked by At

Problem

My TListBox is getting blank when its last TListBoxItem is programatically checked. To illustrate it better, hereby what I mean by getting blank:

enter image description here


Context

I'm generating a list from a TJSONArray. Each item looks like {"event_code","event_name"}.

Then, I compare if the event_code is written on a second TJSONArray : json_response_available_events. If it does, the ListBoxItem will be checked.


Code

procedure TFormHome.TimerGetEventsTimer(Sender: TObject);
var

    K                  : Integer;
    Z                  : Integer;
    ListCount          : Integer;
    AvailableList_Count: Integer;

    lb_item: TListBoxItem;

    event_code_first_array: string;
    event_code  : string;
    event_name  : string;

begin

    // Disable this timer for now
    TimerGetEvents.Enabled := false;

    // Get List of Notifications
    json_response_events           := DM_Auth0.ServerMethods1Client.GetEventsCodeAndDescription(communication_token);
    json_response_available_events := DM_Auth0.ServerMethods1Client.GetAllowedNotificationsList(communication_token, genset_id);

    ListCount                      := json_response_events.Count -1;
    AvailableList_Count            := json_response_available_events.Count - 1;

        for K := 0 to (ListCount) do
        begin

            // Get complete Event Code and Name
            event_name := json_response_events.Items[K].toString;

            // Get Event Code
            event_code_first_array := StringReplace(event_name.Split([':'])[0], '"', '', [rfReplaceAll]);

            // Get Event Name
            event_name   := StringReplace(event_name.Split([':'])[1], '"', '', [rfReplaceAll]);

            // Create ListBoxItem
            lb_item                := TListBoxItem.Create(self);
            lb_item.Parent         := lb_notifications;
            lb_item.Text           := event_name;
            lb_item.StyleLookup    := 'listboxitemleftdetail';

            // Check if this Item code is available
            for Z := 0 to (AvailableList_Count) do
            begin

                if json_response_available_events.Items[Z] <> nil then
                begin

                    // Get Event Code
                    event_code := json_response_available_events.Items[Z].toString;
                    // Format
                    event_code := StringReplace(event_code, '"', '', [rfReplaceAll]);

                    if event_code_first_array.Contains(event_code) then
                    begin

                        if K <= ListCount then
                        begin
                            lb_item.IsChecked  := true;
                            lb_item.IsSelected := false;
                        end;

                    end;

                end;

            end;

        end;

end;

Analysis

If we set to < only, it displays the list correctly but the last item will remain unchecked.

if K < ListCount then
 begin
 lb_item.IsChecked  := true;
 lb_item.IsSelected := false;
end;

I can even change it's properties when its = like

if K = ListCount then
 begin
 lb_item.Text := 'Deadpool for President';
end;

and lb_item.isChecked := false works fine, but when setting lb_item.isChecked := true it gets all weirdly blank.

Why is it happening? And if there's a better way to do what I'm doing, the help will be appreciated.

0

There are 0 best solutions below