Title: How to write your own progressbar in less than 2 minutes
Question: This very short tutorial shows you how to add a progress bar to any web browser programs you make in less than 2 minutes.
It is very basic, and kept as short as possible, you can figure out most of the code just by looking at it. Aimed at beginners, this gives you an easy and quick way to have a working progress bar which informs the user of the progress the web browser is making! You can then focus on the other tasks like for instance the graphical representation to make it the way you want it. Included is the completed example. I hope this little bit of conceptual knowledge will help you
Answer:
How to build a webbrowser with a progressbar in 2 minutes!
1. Start a new project
2. Drop a TWebBrowser component
3. Drop a TEdit component
In the object inspector change its Text property in:
http://www.delphi7.nl
4. Drop a TButton component, doubleclick it and add:
FDownCount:=0;
WebBrowser1.Navigate(Edit1.Text);
5. Drop a TProgressBar
6. In the private section add:
FDownCount: Integer;
7. Select the Webbrowser component, and go to the property inspector
and click the tab Events:
Doubleclick OnDownloadBegin [in the dropdownbox] add:
Inc(FDownCount);
ProgressBar1.Position:=0;
Doubleclick OnDownloadComplete [in the dropdownbox] add:
Dec(FDownCount);
ProgressBar1.Position:=0;
Doubleclick OnProgressChange [in the dropdownbox] add:
if (ProgressMax 0) and (Progress 0) and (FDownCount 0) then
begin
ProgressBar1.Position:=Trunc(Progress / ProgressMax) * 100;
ProgressBar1.Update;
Sleep(100);
Application.ProcessMessages;
end;
8. You are finished now!
For your reference see the below Unit1.pas after it is finished:
/////////////////////////////////////////////////////////////////////
unit Unit1;
/////////////////////////////////////////////////////////////////////
interface
/////////////////////////////////////////////////////////////////////
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ComCtrls, StdCtrls, OleCtrls, SHDocVw;
/////////////////////////////////////////////////////////////////////
type
TForm1 = class(TForm)
Edit1: TEdit;
ProgressBar1: TProgressBar;
Button1: TButton;
WebBrowser1: TWebBrowser;
procedure Button1Click(Sender: TObject);
procedure Webbrowser1DownloadBegin(Sender: TObject);
procedure Webbrowser1DownloadComplete(Sender: TObject);
procedure Webbrowser1ProgressChange(Sender: TObject; Progress,
ProgressMax: Integer);
private
FDownCount: Integer;
public
{ Public declarations }
end;
/////////////////////////////////////////////////////////////////////
var
Form1: TForm1;
/////////////////////////////////////////////////////////////////////
implementation
{$R *.dfm}
/////////////////////////////////////////////////////////////////////
procedure TForm1.Button1Click(Sender: TObject);
begin
FDownCount:=0;
WebBrowser1.Navigate(Edit1.Text);
end;
/////////////////////////////////////////////////////////////////////
procedure TForm1.Webbrowser1DownloadBegin(Sender: TObject);
begin
Inc(FDownCount);
ProgressBar1.Position:=0;
end;
/////////////////////////////////////////////////////////////////////
procedure TForm1.Webbrowser1DownloadComplete(Sender: TObject);
begin
Dec(FDownCount);
ProgressBar1.Position:=0;
end;
/////////////////////////////////////////////////////////////////////
procedure TForm1.Webbrowser1ProgressChange(Sender: TObject; Progress,
ProgressMax: Integer);
begin
if (ProgressMax 0) and (Progress 0) and (FDownCount 0) then
begin
ProgressBar1.Position:=Trunc(Progress / ProgressMax) * 100;
ProgressBar1.Update;
Sleep(100);
Application.ProcessMessages;
end;
end;
/////////////////////////////////////////////////////////////////////
end.
/////////////////////////////////////////////////////////////////////
Easy Progress Bar Writing Guide by Nullified
A Delphi tutorial for writing your own progress bar