Solved

how to add a progessbar in a ListView

Posted on 2001-06-12
4
245 Views
Last Modified: 2010-04-06
Hi all, how can add a progressbar in a TListview component like napstar download listview status or other file sharing programms

thanks chandra
0
Comment
Question by:ChandraZ
  • 2
4 Comments
 
LVL 8

Accepted Solution

by:
Cesario earned 150 total points
ID: 6180968
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
 
LVL 4

Expert Comment

by:DMN
ID: 6181352
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
 
LVL 4

Assisted Solution

by:DMN
DMN earned 150 total points
ID: 6183842
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
 
LVL 14

Expert Comment

by:DragonSlayer
ID: 9286069
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

912 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now