Skip to content

Instantly share code, notes, and snippets.

@melice
Created May 23, 2015 07:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save melice/7e32afce28837dde9828 to your computer and use it in GitHub Desktop.
Save melice/7e32afce28837dde9828 to your computer and use it in GitHub Desktop.
Listview Helper
unit ListviewHelper;
interface
uses System.SysUtils, Vcl.ComCtrls;
type
TListviewHelper = class helper for TListview
public
procedure SetRowCount(Count: Integer; Compacted: Boolean = False);
function UpdateRow(Idx: integer; LvData: TArray<string>;
UpdateCaption: boolean = True): boolean;
end;
implementation
procedure TListviewHelper.SetRowCount(Count: Integer; Compacted: Boolean = False);
var
i: integer;
begin
if Count < 0 then
Count := 0;
while (Items.Count < Count) do
with Items.Add do
for i := 1 to Columns.Count - 1 do
SubItems.Add('');
if Compacted then
for i := Items.Count - 1 downto Count do
Items.Delete(i);
end;
function TListviewHelper.UpdateRow(Idx: integer;
LvData: TArray<string>;
UpdateCaption: boolean = True): boolean;
var
i: Integer;
begin
Result := False;
with Items[Idx] do
begin
if (UpdateCaption) and (Caption <> LvData[0]) then
Caption := LvData[0];
for i := 1 to Columns.Count - 1 do
begin
if (i <= High(LvData)) then
begin
if (SubItems[i - 1] <> LvData[i]) then
SubItems[i - 1] := LvData[i];
end
else
SubItems[i - 1] := '';
end;
end;
end;
end.
@melice
Copy link
Author

melice commented May 23, 2015

usage:
listview1.SetRowCount(100);
for i := 1 to 100 do
listview1.UpdateRow(i-1 ,[ i.tostring , random(100).tostring ]);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment