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

106 86
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 thenbreak;   end; end;

Delphi tips navigator:
» How to "HotTrack" any control
« Scrolling Memo text from code
Subscribe to our newsletter
Sign up here to get the latest news, updates and special offers delivered directly to your inbox.
You can unsubscribe at any time

Leave A Reply

Your email address will not be published.