1. Home
  2. Computing & Technology
  3. Delphi Programming

Abort a Delphi For/While/Repeat Loop by Pressing a Key

By Zarko Gajic, About.com

It is a question that I see many times. There is an option to use a global boolean variable set from an OnClick event. Another one, used here, consists on reading the state of the key in the loop, to know if it is or not pressed...

var
  j : integer;
begin
  for j:=0 to 9999999 do
  begin
   Label1.Caption := IntToStr(j) ;
   {.... loop main body here ...}
   Application.ProcessMessages;

   {ESC key stops the loop}
   if GetKeyState(VK_Escape) AND 128 = 128 then break;
  end;
end;
Delphi tips navigator:
» How to "HotTrack" any control
« Scrolling Memo text from code
Zarko Gajic
Guide since 1998

Zarko Gajic
Delphi Programming Guide

Explore Delphi Programming
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. Delphi Programming
  4. Coding Delphi Applications
  5. Delphi Tips and Tricks
  6. 2000 Delphi Tips
  7. Abort a Delphi For/While/Repeat Loop by Pressing a Key

©2009 About.com, a part of The New York Times Company.

All rights reserved.