Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 256
  • Last Modified:

how to add a progessbar in a ListView

Hi all, how can add a progressbar in a TListview component like napstar download listview status or other file sharing programms

thanks chandra
0
ChandraZ
Asked:
ChandraZ
  • 2
2 Solutions
 
CesarioCommented:
Hi ChandraZ,

Well, you can in fact parent a live progressbar to a listview, you just have to do it at run-time. The sample below has timer that randomly steps the progress bars. The bar is added to the last column of the listview.

There are some gotchas here: the DisplayRect method of a listitem does not return the correct position unless the control is visible, thus the hack used at the top of the method to ensure this. And of course you will have to adjust the width and left bound of the progress bars if the user resizes columns. And you need to add new bars if the user can add items and destroy bars if the user can delete item.



procedure TForm1.FormCreate(Sender: TObject);
var
  pb: TProgressBar;
  r: TRect;
  i, k: Integer;
begin
  Show;
  Application.ProcessMessages;
  for i := 0 to listview1.items.count-1 do begin
    r := listview1.items[i].DisplayRect( drBounds );
    {last column is to take progress bar}
    for k := 1 to listview1.columns.Count -1 do
      r.left := r.left + listview1.columns[k - 1].Width;
      r.right := r.Left + listview1.columns[listview1.columns.Count -1].Width;
    pb := TProgressBar.Create( self );
    pb.Parent := listview1;
    pb.BoundsRect := r;
    listview1.items[i].Data := pb;
  end;
end;

procedure TForm1.Timer1Timer(Sender: TObject);
var
  i: Integer;
  pb: TProgressbar;
begin
  i := Random( listview1.items.count );
  pb := TProgressBar( listview1.Items[i].Data );
  if assigned(pb) then
    if pb.Position = pb.Max then
      pb.Position := 0
    else
      pb.StepBy( pb.Max div 10 );
end;


Best Regards

Cesario
0
 
DMNCommented:
Really you need to add one additional subitem which holds persentage (like Item.Subitems.Add('33')) and use OnDrawSubitem event to draw progress bar. Search PAQ's for my previous solution...
0
 
DMNCommented:
Here is working sample. Start new project, drop listview on form. Do not forget to set Form's OnCreate to FormCreate and listview's OnCustomDrawSubitem to Listview1Customdrawsubitem.
================================

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  CommCtrl, ComCtrls;

type
  TForm1 = class(TForm)
    ListView1: TListView;
    procedure FormCreate(Sender: TObject);
    procedure ListView1CustomDrawSubItem(Sender: TCustomListView;
      Item: TListItem; SubItem: Integer; State: TCustomDrawState;
      var DefaultDraw: Boolean);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.FormCreate(Sender: TObject);
var
  i:integer;
begin
  with ListView1 do
    begin
      ViewStyle := vsReport;
      Columns.Clear;
      with Columns.Add do
        begin
          Caption := 'Name';
          Width := 150;
        end;
      with Columns.Add do
        begin
          Caption := 'Progress';
          Width := 120;
        end;
      for i:=0 to 9 do
        with Items.Add do
          begin
            Caption := 'Item '+IntToStr(i);
            Subitems.Add(IntToStr(i*10));
          end;
    end;
end;

procedure TForm1.ListView1CustomDrawSubItem(Sender: TCustomListView;
  Item: TListItem; SubItem: Integer; State: TCustomDrawState;
  var DefaultDraw: Boolean);
var
  r:TRect;
  i:integer;
begin
  if SubItem=1 then
    begin
      DefaultDraw := False;
      ListView_GetItemRect(Sender.Handle,Item.Index,R,LVIR_BOUNDS);
      for i:=0 to SubItem-1 do
        r.Left := r.Left + ListView_GetColumnWidth(Sender.Handle,i);
      r.Right := r.Left + ListView_GetColumnWidth(Sender.Handle,Subitem);
      InflateRect(r,-1,-1);
      Sender.Canvas.Pen.Color := clBlack;
      Sender.Canvas.Brush.Color := clWhite;
      Sender.Canvas.Rectangle(r);
      InflateRect(r,-1,-1);
      R.Right := (R.Right-R.Left)*StrToInt(Item.Subitems[SubItem-1])div 100+R.Left;
      Sender.Canvas.Brush.Color := clBlue;
      Sender.Canvas.FillRect(r);
    end;
end;

end.
0
 
DragonSlayerCommented:
ChandraZ,
No comment has been added lately (814 days), so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area for this question:

RECOMMENDATION: split points between Cesario http:#6180968 and DMN http:#6183842

Please leave any comments here within 7 days.

-- Please DO NOT accept this comment as an answer ! --

Thanks,

DragonSlayer
EE Cleanup Volunteer
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now