Recent

Author Topic: I want to add a progress bar but I don't know the correct way to do it.  (Read 1472 times)

AfootDesert41

  • New Member
  • *
  • Posts: 15
Hello programmers! I am recently working on a college project where I have to develop a GUI app that simulates a very basic supermarket cashing system without Inventory database (basically you have to make up all the items you add) that will generate a on-screen sale invoice. The thing is that I want to add a "Printing, please wait..." window with a progress bar. I've tried implementing this by myself but I've failed to do it the way I want, I added a form wich there is a Ttimer (interval:=100) with a TprogressBar Below, this is the code I made for the progress bar to show, load, end and show the final form (and close the previous one) where the invoice is supposed show:

Code: Pascal  [Select][+][-]
  1. procedure TVentanaImprimiendoFactura.Timer1Timer(Sender: TObject);
  2. begin
  3.   ProgressBar1.Position:=ProgressBar1.Position+5;
  4.   If ProgressBar1.Position=100 then
  5.   VentanaFactura.Show;
  6.   VentanaImprimiendoFactura.Close;
  7. end;
  8.  

I dont exactly know how Ttimer works, but when I try to Debug a second time and so on, the Invoice form will show seconds after I start the debug, also the form wont close when I press the X (close window button), or it closes but it opens again, I want the last form to be able to be closed so I can generate Invoices for other costumers.
« Last Edit: December 26, 2018, 04:04:28 am by AfootDesert41 »

wadman

  • New Member
  • *
  • Posts: 37
    • wadman's home
Re: I want to add a progress bar but I don't know the correct way to do it.
« Reply #1 on: December 26, 2018, 06:36:38 am »

procedure TVentanaImprimiendoFactura.Timer1Timer(Sender: TObject);
begin
  ProgressBar1.Position:=ProgressBar1.Position+5;
  If ProgressBar1.Position>=100 then begin
    Timer1.Enabled := false;
    VentanaImprimiendoFactura.Close;
    VentanaFactura.Show;
  end;
end;


I would write something like this.

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: I want to add a progress bar but I don't know the correct way to do it.
« Reply #2 on: December 26, 2018, 07:38:56 am »
Explanation

With the code written as it should the first error is easier to see:
Code: Pascal  [Select][+][-]
  1. procedure TVentanaImprimiendoFactura.Timer1Timer(Sender: TObject);
  2. begin
  3.   ProgressBar1.Position:=ProgressBar1.Position+5;
  4.   If ProgressBar1.Position=100 then
  5.       VentanaFactura.Show;
  6.   VentanaImprimiendoFactura.Close;
  7. end;

The window closes with the first timer event. It should be:

Code: Pascal  [Select][+][-]
  1. TVentanaImprimiendoFactura.Timer1Timer(Sender: TObject);
  2. begin
  3.   ProgressBar1.Position:=ProgressBar1.Position+5;
  4.   If ProgressBar1.Position=100 then
  5.   begin
  6.       VentanaFactura.Show;
  7.       VentanaImprimiendoFactura.Close;
  8.   end;
  9. end;

The second error is that you didn't stop the timer, so it kept ticking away and re-executing the handler. Which makes us note the third error: the comparison ProgressBar1.Position = 100 is too restrictive: it may happen just once ... or never! P.e. suppose you started by setting it to 1.

Taking all into account we end up with wadman's more correct code:
Code: Pascal  [Select][+][-]
  1. procedure TVentanaImprimiendoFactura.Timer1Timer(Sender: TObject);
  2. begin
  3.   ProgressBar1.Position:=ProgressBar1.Position+5;
  4.   If ProgressBar1.Position >= 100 then begin
  5.     Timer1.Enabled := false;
  6.     VentanaImprimiendoFactura.Close;
  7.     VentanaFactura.Show;
  8.   end;
  9. end;

HTH!
« Last Edit: December 26, 2018, 07:41:51 am by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

AfootDesert41

  • New Member
  • *
  • Posts: 15
Re: I want to add a progress bar but I don't know the correct way to do it.
« Reply #3 on: December 26, 2018, 08:08:04 am »
Thank you so much for the replies! :)

 

TinyPortal © 2005-2018