Solved

how to add a progessbar in a ListView

Posted on 2001-06-12
4
243 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
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…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

772 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

11 Experts available now in Live!

Get 1:1 Help Now