Recent

Author Topic: caption update in form ?  (Read 8083 times)

cstrasser11

  • New Member
  • *
  • Posts: 11
caption update in form ?
« on: January 05, 2011, 04:21:18 am »
hi ... code below ...seems simple enough but does not work as i expect ... the caption is not updated until the loop is finished.  how can i get the label to show each iteration of x ?


procedure TForm1.Button1Click(Sender: TObject);
var x : integer;
begin
label1.Caption:= '===';
  for x:=1 to 25 do
   begin
    label1.Caption:= inttostr(x);
    sleep(1000);
   end;
end;                           

Phil

  • Hero Member
  • *****
  • Posts: 2737
Re: caption update in form ?
« Reply #1 on: January 05, 2011, 04:39:30 am »
Have you tried calling Update to update the form after each change to Caption?

Are you trying to create a progress indicator or something? Don't forget that there's TProgressBar for that.

Thanks.

-Phil

cdbc

  • Hero Member
  • *****
  • Posts: 2664
    • http://www.cdbc.dk
Re: caption update in form ?
« Reply #2 on: January 05, 2011, 08:43:14 am »
Hi

procedure TForm1.Button1Click(Sender: TObject);
var x : integer;
begin
label1.Caption:= '===';
  for x:=1 to 25 do
   begin
    label1.Caption:= inttostr(x);
    Application.Processmessages;
    sleep(1000);
   end;
end;

Should do the trick, just a simple hack  :)

hth - Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE6/QT6 -> FPC Release -> Lazarus Release &  FPC Main -> Lazarus Main

cstrasser11

  • New Member
  • *
  • Posts: 11
Re: caption update in form ?
« Reply #3 on: January 05, 2011, 04:27:36 pm »
Hey thanks.. as soon as i added the application processmessages line all worked as expected. I am new at programming with lazarus/windows (old DOS turbo pascal programmer) so there is much that i don't even know that i don't know...  if you know what i mean ... that said how do i call to update the form ?

thanks again :-\

cdbc

  • Hero Member
  • *****
  • Posts: 2664
    • http://www.cdbc.dk
Re: caption update in form ?
« Reply #4 on: January 05, 2011, 11:53:01 pm »
Hi

In your event-handler, call: Form1.update; Mind you, that doesn't update your form / control. It just puts your request in the message queue, which will be processed when the application has time to do it... Hence the Application.ProcessMessages; call shown before, it forces the application to process the messagequeue, not very efficient though...

hth - Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE6/QT6 -> FPC Release -> Lazarus Release &  FPC Main -> Lazarus Main

 

TinyPortal © 2005-2018