[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

how to add a progessbar in a ListView

Posted on 2001-06-12
4
Medium Priority
?
253 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 600 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 600 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

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…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

656 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