I'm still a little fuzzy with using pointers, and although I thought I had this down right, I'm having a strange issue. I have a TListView. The items are added in run-time when the app starts and the data is also assigned to each item. Then I have a thread which runs in the background. The thread reads the database and gets some specific information. When new info is available in the thread, it triggers an event which tells the app to refresh the item to reflect the new info.
The thread IS triggering the proper event with no doubt. The trouble is when it comes to identifying which TListItem in the TListView needs to be updated. For some reason, only the first item in the list gets updated, all the rest remain untouched. It's like the loop which looks for the item can only find the first item, not the others...
Declaration of type stored in item data pointer:
Populating items of list:
And refreshing an item in the list:
Now the problem I'm facing is that only the first item gets refreshed, all the rest are not. In the loop, I'm checking each item to find the item which I need to refresh. The caption is then updated to reflect the new information (which comes from a thread). The procedure "ThreadAlert" is an event handler which comes from this thread, triggered when there's new information available (thus needing to refresh that info).
JD Solutions
The thread IS triggering the proper event with no doubt. The trouble is when it comes to identifying which TListItem in the TListView needs to be updated. For some reason, only the first item in the list gets updated, all the rest remain untouched. It's like the loop which looks for the item can only find the first item, not the others...
Declaration of type stored in item data pointer:
Code:
PAlertType = ^TAlertType;
TAlertType = (atSalesMonthLast, atSalesMonthYear, atSalesQuarter,
atSalesYear, atOverdueApprovals, atUnlinkedBO, atPODueToday,
atOverduePO, atPurchConsign, atPickupDelivery);
Populating items of list:
Code:
procedure TfrmMain.LoadListItems;
var
X: Integer;
I: TListItem;
procedure A(const S: String; const T: TAlertType);
begin
I:= Lst.Items.Add;
I.Caption:= S;
I.Data:= @T; //DATA ASSIGNED HERE
end;
begin
Lst.Items.Clear;
//Eventually I will be reading user preferences here
// for which items to list and what order to list them,
// therefore I need to use the pointer to know which item is which.
// Otherwise, if the list never changes, I wouldn't care about this.
A('This month vs last month', atSalesMonthLast);
A('This month vs this month last year', atSalesMonthYear);
A('This quarter vs this quarter last year', atSalesQuarter);
A('This year vs last year', atSalesYear);
A('Overdue Approval Invoices', atOverdueApprovals);
A('Unlinked Back Order Items', atUnlinkedBO);
A('Purchase Orders Due Today', atPODueToday);
A('Overdue Purchase Orders', atOverduePO);
A('Consignments to be Purchased', atPurchConsign);
A('Pickups and Deliveries', atPickupDelivery);
end;
And refreshing an item in the list:
Code:
procedure TfrmMain.ThreadAlert(Sender: TObject; const T: TAlertType;
const S: String; const ID: Integer);
var
I: TListItem;
A: PAlertType;
X: Integer;
begin
//Find the item in the list which needs to be updated
for X:= 0 to Lst.Items.Count - 1 do begin
I:= Lst.Items[X];
A:= PAlertType(I.Data); //DATA READ HERE
if A^ = T then begin //COMPARE ITEM DATA WITH DESIRED VALUE
I.Caption:= S;
I.ImageIndex:= ID;
Break;
end;
end;
end;
Now the problem I'm facing is that only the first item gets refreshed, all the rest are not. In the loop, I'm checking each item to find the item which I need to refresh. The caption is then updated to reflect the new information (which comes from a thread). The procedure "ThreadAlert" is an event handler which comes from this thread, triggered when there's new information available (thus needing to refresh that info).
JD Solutions