Hello!
I have a problem with a runtime created button in my program which can be controllable by keys. When the user press ESC the program halts, with any other keys the program changes its status:
if the actual screen is the title screen (2) then jump to first level (3). I create this exit button at key down on the title screen and change to the other screen. So far - everything is OK.
But when I click on this exit button and select NO (=cancel) instead of YES (=exit) and after this I try to activate this button with ESC key - nothing happening.
Therefore I complement piece of code of exit button create with this line:
exitbutton.OnKeyDown:=@FormKeyDown;But after this I get an error message, as you can see on the attached image. Maybe
@FormKeyDown cannot be in
FormKeyDown event?
I tried to replace piece of code of exit button create into
FormCreate event and make this button invisible and disabled, then just enable these two properties in
FormKeyDown in the previous place, but the situation is the same.
The first solution would be the best for me, that is creating this button at
FormKeyDown event.
But what should I do to prevent that error and allow using ESC key with that button?
procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState
);
begin
if Key=VK_ESCAPE then
exitbuttonclick(form1)
else
begin
case gamestatus of
2: begin
exitbutton:=TButton.Create(form1);
exitbutton.Parent:=form1;
exitbutton.Top:=2;
exitbutton.Left:=2;
exitbutton.Caption:='Esc';
exitbutton.OnKeyDown:=@FormKeyDown;
exitbutton.OnClick:=@exitbuttonclick;
(...)
gamestate=3;
end;
And the
exitbuttonclick procedure:
procedure TForm1.exitbuttonclick(Sender: TObject);
var answer,stilus: integer;
begin
stilus:=MB_ICONQUESTION + MB_YESNO;
answer:=Application.MessageBox('Are you sure you want to exit?', 'Confirmation', stilus);
if answer=IDYES then
if gamestatus=3 then
begin
clearallobjects();
gamestatus:=0;
FreeAndNil(exitbutton);
end
else Close;
end;