Recent

Author Topic: (solved)How to animate label ellipsis?  (Read 755 times)

ShungFeng

  • New Member
  • *
  • Posts: 20
(solved)How to animate label ellipsis?
« on: February 20, 2020, 12:20:56 am »
Hey Guys,

I'm trying to make a label have an animated ellipsis (three dots), based on a timer's tick, basically just to show that something is happening:

Label_Text
Label_Text.
Label_Text..
Label_Text...
Label_Text
Label_Text.
Label_Text..
Label_Text...
etc...

I'm really new to programming, so what I tried, no surprise, didn't work:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Timer1Timer(Sender: TObject);
  2. begin
  3.   if Label1.Caption = 'hello...' then
  4.     label1.Caption := 'hello'
  5.   else if label1.Caption = 'hello.' then
  6.     label1.Caption := 'hello..'
  7.   else if label1.Caption = 'hello..' then
  8.     label1.Caption := 'hello...';
  9. end;                                  

Could someone help me out with the logic?

Thanks very much.
« Last Edit: February 20, 2020, 01:11:36 pm by ShungFeng »

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: How to animate label ellipsis?
« Reply #1 on: February 20, 2020, 12:32:32 am »
Hi!

Do this:

Code: Pascal  [Select][+][-]
  1. ....
  2. implementation
  3. var msg : String = 'Hello crazy world! ';
  4. .....
  5.  
  6. procedure TForm1.Timer1Timer(Sender: TObject);
  7. var tmp : String;
  8.  
  9. begin
  10.   tmp := Utf8Copy (msg,1,1);
  11.   Utf8Delete (msg,1,1);
  12.   msg := msg + tmp;
  13.   Label1.Caption := msg;
  14. end;
  15.  
  16.  

Have fun!

Winni

Handoko

  • Hero Member
  • *****
  • Posts: 5151
  • My goal: build my own game engine using Lazarus
Re: How to animate label ellipsis?
« Reply #2 on: February 20, 2020, 03:07:31 am »
@ShungFeng

Actually your logic works, you just forgot something. Here is the corrected version:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Timer1Timer(Sender: TObject);
  2. begin
  3.   if Label1.Caption = 'hello...' then
  4.     label1.Caption := 'hello'
  5.   else if Label1.Caption = 'hello' then
  6.     label1.Caption := 'hello.'
  7.   else if label1.Caption = 'hello.' then
  8.     label1.Caption := 'hello..'
  9.   else if label1.Caption = 'hello..' then
  10.     label1.Caption := 'hello...';
  11. end;

And I recommend you this more readable version:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Timer1Timer(Sender: TObject);
  2. begin
  3.   case Label1.Caption of
  4.     'hello...': Label1.Caption := 'hello';
  5.     'hello'   : Label1.Caption := 'hello.';
  6.     'hello.'  : Label1.Caption := 'hello..';
  7.     'hello..' : Label1.Caption := 'hello...';
  8.   end;
  9. end;

jamie

  • Hero Member
  • *****
  • Posts: 6130
Re: How to animate label ellipsis?
« Reply #3 on: February 20, 2020, 03:15:11 am »
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Timer1Timer(Sender: TObject);
  2. Const C:Integer = 1;
  3. Var
  4.   S:String;
  5.   X:Integer;
  6. begin
  7.   S := '';
  8.   For X := 1 To C do S := S+'.';
  9.   Label1.Caption := 'Please Wait'+S;
  10.   C := (C + 1)and $03;
  11.   If C = 0 Then Inc(X);
  12.   Label1.Repaint;
  13. end;                        
  14.  

For this to work, you need to call "Application.ProcessMessages" in some large loop you are processing so that this will have a chance to work..

You may also notice I used the REPAINT method, this is needed...too to get a clean paint on the screen...
The only true wisdom is knowing you know nothing

ShungFeng

  • New Member
  • *
  • Posts: 20
Re: How to animate label ellipsis?
« Reply #4 on: February 20, 2020, 01:11:21 pm »
Thanks guys, I used jamie's solution in the end.

 

TinyPortal © 2005-2018