Solved

how to add a progessbar in a ListView

Posted on 2001-06-12
4
250 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
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…
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

717 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