Created
May 23, 2015 07:15
-
-
Save melice/7e32afce28837dde9828 to your computer and use it in GitHub Desktop.
Listview Helper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
usage:
listview1.SetRowCount(100);
for i := 1 to 100 do
listview1.UpdateRow(i-1 ,[ i.tostring , random(100).tostring ]);