ListView not updating when dragging its scrollbar thumb?

130 Views Asked by At

Even though the timer is constantly adding new items, this is NOT shown while dragging the listview's scrollbar thumb.

(Does update as expected when a custom style has been set for the listview)

How to force the listview to update properly?

Reproduction:

var
  I: Integer;

procedure TForm1.Timer1Timer(Sender: TObject);
begin
  I := I + 1;
  ListView1.Items.Add.caption := IntToStr(I);
end;
1

There are 1 best solutions below

1
Tom Brunberg On

In addition to the text of your question, you also say in a comment that "... the idea is to scroll it (the TListView) to the end after a new item has been added IF previously at bottom".

The following does that:

var
  I: integer;

procedure TForm7.Timer1Timer(Sender: TObject);
var
  KeepLastItemVisible: boolean;
begin
  // Check if scrolled to the end of list before adding a new item
  KeepLastItemVisible := false;
  with ListView1 do
    with Items do
      if Count > 1 then
        if Item[Count-1].index = TopItem.Index - 1 + VisibleRowCount then
          KeepLastItemVisible := true;

  // Add new item
  I := I + 1;
  ListView1.Items.Add.caption := IntToStr(I);

  // Conditionally force scrolling last row into view
  if KeepLastItemVisible then
    with ListView1.Items do
      Item[Count-1].MakeVisible(False);
end;